LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-02-2020, 12:59 AM   #1
sandeep.c
LQ Newbie
 
Registered: Oct 2020
Posts: 3

Rep: Reputation: Disabled
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

Last edited by sandeep.c; 10-02-2020 at 01:08 AM.
 
Old 10-02-2020, 02:08 AM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,517
Blog Entries: 11

Rep: Reputation: 3473Reputation: 3473Reputation: 3473Reputation: 3473Reputation: 3473Reputation: 3473Reputation: 3473Reputation: 3473Reputation: 3473Reputation: 3473Reputation: 3473
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!
 
1 members found this post helpful.
Old 10-02-2020, 02:52 AM   #3
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 16,688
Blog Entries: 10

Rep: Reputation: 4924Reputation: 4924Reputation: 4924Reputation: 4924Reputation: 4924Reputation: 4924Reputation: 4924Reputation: 4924Reputation: 4924Reputation: 4924Reputation: 4924
Quote:
Originally Posted by sandeep.c View Post
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
 
1 members found this post helpful.
Old 10-02-2020, 09:25 AM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 23,425

Rep: Reputation: 6647Reputation: 6647Reputation: 6647Reputation: 6647Reputation: 6647Reputation: 6647Reputation: 6647Reputation: 6647Reputation: 6647Reputation: 6647Reputation: 6647
Quote:
Originally Posted by sandeep.c View Post
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?
 
1 members found this post helpful.
Old 10-02-2020, 10:26 AM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,568

Rep: Reputation: 710Reputation: 710Reputation: 710Reputation: 710Reputation: 710Reputation: 710Reputation: 710
One mistake fixed:
Code:
base=$(basename "$pathvariable")
dir=$(dirname "$pathvariable")
 
1 members found this post helpful.
Old 10-02-2020, 10:34 AM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,160
Blog Entries: 13

Rep: Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224
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"
 
2 members found this post helpful.
Old 10-03-2020, 05:41 AM   #7
sandeep.c
LQ Newbie
 
Registered: Oct 2020
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by astrogeek View Post
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
 
Old 10-04-2020, 10:10 AM   #8
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 23,425

Rep: Reputation: 6647Reputation: 6647Reputation: 6647Reputation: 6647Reputation: 6647Reputation: 6647Reputation: 6647Reputation: 6647Reputation: 6647Reputation: 6647Reputation: 6647
Quote:
Originally Posted by sandeep.c View Post
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/
 
Old 10-04-2020, 12:06 PM   #9
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 292

Rep: Reputation: Disabled
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 .
 
1 members found this post helpful.
Old 10-05-2020, 05:33 AM   #10
sandeep.c
LQ Newbie
 
Registered: Oct 2020
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TB0ne View Post
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,
 
Old 10-05-2020, 12:30 PM   #11
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 23,425

Rep: Reputation: 6647Reputation: 6647Reputation: 6647Reputation: 6647Reputation: 6647Reputation: 6647Reputation: 6647Reputation: 6647Reputation: 6647Reputation: 6647Reputation: 6647
Quote:
Originally Posted by sandeep.c View Post
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.

Last edited by TB0ne; 10-05-2020 at 12:33 PM.
 
Old 10-07-2020, 02:02 AM   #12
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 16,688
Blog Entries: 10

Rep: Reputation: 4924Reputation: 4924Reputation: 4924Reputation: 4924Reputation: 4924Reputation: 4924Reputation: 4924Reputation: 4924Reputation: 4924Reputation: 4924Reputation: 4924
Quote:
Originally Posted by sandeep.c View Post
only a need to know basis on scripts is required
I think you need to know more about shell scripting in general.

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.
 
1 members found this post helpful.
Old 10-08-2020, 04:31 PM   #13
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Older: Coherent, MacOS, Red Hat, Big Iron IXs: AIX, Solaris, Tru64
Posts: 2,267

Rep: Reputation: 408Reputation: 408Reputation: 408Reputation: 408Reputation: 408
Quote:
Originally Posted by TB0ne View Post
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.
 
1 members found this post helpful.
Old 10-08-2020, 05:43 PM   #14
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 23,425

Rep: Reputation: 6647Reputation: 6647Reputation: 6647Reputation: 6647Reputation: 6647Reputation: 6647Reputation: 6647Reputation: 6647Reputation: 6647Reputation: 6647Reputation: 6647
Quote:
Originally Posted by rnturn View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Must PS1 string be always reevaluated each time input enter BudiKusasi Linux - Software 2 08-14-2020 03:10 AM
LXer: Technology Changes Us, Changes Society, and Changes Governments LXer Syndicated Linux News 0 10-10-2016 03:00 PM
[SOLVED] Write a shell script that receives a word, an input file and an output file. The scripts copies all the lines in the input file that contain mandy2112 Linux - Newbie 3 08-18-2016 11:11 AM
User input into Bash scripts and checking validity of user input?? helptonewbie Programming 8 07-07-2008 07:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration