LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   shell script to take input from user , a list of directory path into file and enter each directory make changes to file (https://www.linuxquestions.org/questions/programming-9/shell-script-to-take-input-from-user-a-list-of-directory-path-into-file-and-enter-each-directory-make-changes-to-file-4175683055/)

sandeep.c 10-01-2020 11:59 PM

shell script to take input from user , a list of directory path into file and enter each directory make changes to file
 
Requirement---URGENT for production server----------please check for my syntaxes if possible-------------

echo path

cat >> path.txt

/app/tomcat7/webapps/sessionchecking.class ----- directory paths may increase in size or number ------
/app1/tomcat1/webapps1/Home.jsp
/app2/tomcat2/webapps2/struts.xml

ctrl-d

cat path.txt | while read line
do
pathvariable="$line"

base=$(basename ($pathvariable))
dir=$(dirname ($pathvariable))
echo $base
echo $dir

cd "$dir" -------------need to clear the contents of dir after processing -----------
mv $base /Contexts/backup
scp 10.3.x.x:/root/Downloads/sessionchecking.class .
done

astrogeek 10-02-2020 01:08 AM

Welcome to LQ!

While your requirements may be urgent to yourself, please understand that members here volunteer their time and may choose which questions they wish to reply to, and when. If this is related to your work as it appears to be, you would be better advised to seek help from a coworker who would be familiar with the application environment and any operational constraints.

If the script is not working as expected, please describe how it is invoked and how operation differs from what you expect. Be sure to include any error messages received.

Please review the Site FAQ for guidance in posting your questions and general forum usage. Especially, read the link in that page, How To Ask Questions The Smart Way. The more effort you put into understanding your problem and framing your questions, the better others can help!

ondoho 10-02-2020 01:52 AM

Quote:

Originally Posted by sandeep.c (Post 6171789)
Requirement---URGENT for production server----------please check for my syntaxes if possible-------------

How extremely impolite!

Please edit your first post, replace this line with a humble request without the word "urgent", and put your code in CODE tags. You also need to tell it (and us) which shell you're using, e.g.
Code:

#!/bin/tcsh

TB0ne 10-02-2020 08:25 AM

Quote:

Originally Posted by sandeep.c (Post 6171789)
Requirement---URGENT for production server

Incorrect; this is NOT URGENT for anyone here, and it isn't for any of our production servers.
Quote:

----------please check for my syntaxes if possible-------------
Check syntax for what?? You haven't actually said what (if anything) is wrong. And if you were hired to do work on a server, then shouldn't you be able to debug/correct a simple shell script on your own?
Quote:

Code:

echo path
cat >> path.txt

/app/tomcat7/webapps/sessionchecking.class    ----- directory paths may increase in size or number ------     
/app1/tomcat1/webapps1/Home.jsp
/app2/tomcat2/webapps2/struts.xml

ctrl-d

cat path.txt | while read line
do
pathvariable="$line"

base=$(basename ($pathvariable))
dir=$(dirname ($pathvariable))
echo $base
echo $dir

cd "$dir"                        -------------need to clear the contents of dir  after processing -----------
mv $base /Contexts/backup
scp 10.3.x.x:/root/Downloads/sessionchecking.class .
done


Since this is 'urgent', have you tried just running this script to see if it works?

MadeInGermany 10-02-2020 09:26 AM

One mistake fixed:
Code:

base=$(basename "$pathvariable")
dir=$(dirname "$pathvariable")


rtmistler 10-02-2020 09:34 AM

Besides the guidance already given, suggest you use Shell Check which is a site where you can post your script and it will provide feedback for exactly what you're asking, a syntax check.

But bear in mind that you can check your own syntax, by running your script and also doing things like enabling verbose output and debug with lines like "set -xv"

sandeep.c 10-03-2020 04:41 AM

Quote:

Originally Posted by astrogeek (Post 6171797)
Welcome to LQ!

While your requirements may be urgent to yourself, please understand that members here volunteer their time and may choose which questions they wish to reply to, and when. If this is related to your work as it appears to be, you would be better advised to seek help from a coworker who would be familiar with the application environment and any operational constraints.

If the script is not working as expected, please describe how it is invoked and how operation differs from what you expect. Be sure to include any error messages received.

Please review the Site FAQ for guidance in posting your questions and general forum usage. Especially, read the link in that page, How To Ask Questions The Smart Way. The more effort you put into understanding your problem and framing your questions, the better others can help!



I apologize , i was unaware of the question guidelines

And to be frank i am a server admin , and a complete newbie to world of scripting

But In due course of my work I found out shell scripting to be very useful in automating server work and i am in my learning process
Hence this script is something I have prepared reading some programs and scripts and tutorials online
I would like to thank for pointing out the syntax and please guide me for the above script as i have been struggling from long time

query : the path app/tomcat7/webapps is taken as a string in to the variable but

when we are entering with the help of script into directory ex : < cd /app/tomcat7/webapps > its echoing pwd as /app/tomcat7/webapps which is correct
But with a while loop its not entering into the second path < cd /app1/tomcat1/webapps1 > and not echoing the $PWD variable

TB0ne 10-04-2020 09:10 AM

Quote:

Originally Posted by sandeep.c (Post 6172193)
I apologize , i was unaware of the question guidelines And to be frank i am a server admin , and a complete newbie to world of scripting

How did you get a job as a server admin if you can't write a script??
Quote:

But In due course of my work I found out shell scripting to be very useful in automating server work and i am in my learning process Hence this script is something I have prepared reading some programs and scripts and tutorials online I would like to thank for pointing out the syntax and please guide me for the above script as i have been struggling from long time

query : the path app/tomcat7/webapps is taken as a string in to the variable but

when we are entering with the help of script into directory ex : < cd /app/tomcat7/webapps > its echoing pwd as /app/tomcat7/webapps which is correct
But with a while loop its not entering into the second path < cd /app1/tomcat1/webapps1 > and not echoing the $PWD variable
Putting "bash script read a file" into Google pulls up a LOT...the link to a bash scripting tutorial is in my posting signature, and all of this is covered. You don't say what tutorials you're following, but it's hard to know why you'd be struggling for a long time with a very basic operation.

Example: https://www.cyberciti.biz/faq/unix-h...ine-from-file/

pedropt 10-04-2020 11:06 AM

I could be wrong but if the depth where the files you need may change and if the files you are requesting have always the same name then why not :

- use find command to search for the file in app directory
- retrieve the full path of the file from find output
- and then move the file to where you want or do whatever you need ahead after having the correct locations of the files .

sandeep.c 10-05-2020 04:33 AM

Quote:

Originally Posted by TB0ne (Post 6172508)
How did you get a job as a server admin if you can't write a script??

Putting "bash script read a file" into Google pulls up a LOT...the link to a bash scripting tutorial is in my posting signature, and all of this is covered. You don't say what tutorials you're following, but it's hard to know why you'd be struggling for a long time with a very basic operation.

Example: https://www.cyberciti.biz/faq/unix-h...ine-from-file/

That's because I am in to server administration configuration and troubleshooting in my job role , u don't need to know programming to get a job in server administration in India , there are bunch of python and ansible guys to do that , only a need to know basis on scripts is required but scripts are what saving me a lot on my work ,hence I am into this now,

TB0ne 10-05-2020 11:30 AM

Quote:

Originally Posted by sandeep.c (Post 6172720)
That's because I am in to server administration configuration and troubleshooting in my job role , u don't need to know programming to get a job in server administration in India , there are bunch of python and ansible guys to do that , only a need to know basis on scripts is required but scripts are what saving me a lot on my work ,hence I am into this now,

Read the LQ Rules about not using text-speak. This isn't a top-secret thing that's 'need-to-know', and since you want to do it, you then need to know, right???

Again, you were given links to tutorials showing you how to do what you want.

ondoho 10-07-2020 01:02 AM

Quote:

Originally Posted by sandeep.c (Post 6172720)
only a need to know basis on scripts is required

I think you need to know more about shell scripting in general.
:D
Two sites were already recommended, here's a third:
http://mywiki.wooledge.org/BashGuide
Contains answers to your question.

Ans you still didn't remove the insult from your first post.
Nor did you put your code in CODE tags.

rnturn 10-08-2020 03:31 PM

Quote:

Originally Posted by TB0ne (Post 6172508)
How did you get a job as a server admin if you can't write a script??

Hey... back in my VMS days, you'd be surprised how many headhunters would actually ask system manager candidates the silly question: "Do you know DCL?" IBM was not requiring UNIX admins to know anything about scripting (or any other kind of programming, for that matter) because they just about prohibit them from writing any kind of code---the contract with their clients may not include writing software -- of any kind -- as a billable service.

TB0ne 10-08-2020 04:43 PM

Quote:

Originally Posted by rnturn (Post 6173769)
Hey... back in my VMS days, you'd be surprised how many headhunters would actually ask system manager candidates the silly question: "Do you know DCL?" IBM was not requiring UNIX admins to know anything about scripting (or any other kind of programming, for that matter) because they just about prohibit them from writing any kind of code---the contract with their clients may not include writing software -- of any kind -- as a billable service.

I remember DCL (and TCL) fondly...I managed a herd of Vaxen at one time. Still one of the best platforms I've ever worked on.

That said...things are different now. While I totally get that a contract company doesn't want their folks shoveling out software off the books, doing a basic bash script for automating a process is different.


All times are GMT -5. The time now is 03:51 AM.