Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi.......everybody. I just need a shell script to answer our questions and it should run same script if the user say yes otherwise it should exit with showing an output of the file when user press n or no. For that I have made a script as below
#!/bin/bash
y=2
n=1
cd /root
echo "" >> logbook && clear
echo What is your name?
read name && echo $name, `date` > /tmp/logbook && clear
echo Write your administration work that will be stored in /root/logbook file, Use "ctrl+d" to terminate the session
cat >> /tmp/logbook && cat /tmp/logbook >> logbook
echo Do you want write one more log?
read answer
$read=1
if [ $read -eq $y ]; then
echo These are your recent jobs, Bye!
cat /tmp/logbook
else
/home/ananth/test1.sh
fi
#END
From above script I am on confuse, because it is continuously running the same script even I press "y" or "n". I am not able to exit the script by pressing n. So could anybody help me out this.
Click here to see the post LQ members have rated as the most helpful post in this thread.
Welcome to LQ, however late I am in saying it. I hope your time here helps you as much as mine has helped me.
Did you find a solution yet?
If so, please post it for others to benefit from.
What is an "LKG student in scrpting"?
Some pointers for using LQ effectively:
Please don't write chat/IM/leet style like "u" & "i", please take the time to write out "you" & "I". Your posts here are permanent & deserve more time & attention than in those media. TIA.
Please put code, command line output, config files, etc. inside [CODE] tags, aka "Code:" blocks.
It will make your posts easier to read, & that will get you more, faster, better answers. -- Help us help you.
BTW, You can edit your post(s) to do this retroactively. Please do so soon. If you need help doing this, please ask.
Thanks for you co-operation.
Is this in fact homework? Remember that we are happy to help guide to an answer, but not actually do it for you.
BTW, your script is a very good beginner's effort, & that makes the prospect of helping you more fun.
szboardstretcher's comment seems clear to me -- he's telling where to look for the logic & syntax errors that are keeping your script from working. More on this in my next post.
I have numbered my questions, please feel free to respond by number & not have to clutter your reply by quoting questions.
Here a copy of your script w/ line #s & highlighting:
Code:
1 #!/bin/bash
2 y=2
3 n=1
4 cd /root
5 echo "" >> logbook && clear
6 echo What is your name?
7 read name && echo $name, `date` > /tmp/logbook && clear
8 echo Write your administration work that will be stored in /root/logbook file, Use "ctrl+d" to terminate the session
9 cat >> /tmp/logbook && cat /tmp/logbook >> logbook
10 echo Do you want write one more log?
11 read answer
12 $read=1
13 if [ $read -eq $y ]; then
14 echo These are your recent jobs, Bye!
15 cat /tmp/logbook
16 else
17 /home/ananth/test1.sh
18 fi
19 #END
Most of what follows is minor, & included to try to help you improve. Hope it's helpful.
general: did you use set -x in debugging this?
2&3: These variables are unnecessary & the values you assign are non-standard.
4: You never cd back, this caused problems on my system, & may cause them where you would install this.
5: The '""' (double quotes) are unnecessary.
5, 7, 9: Good use of '&&'.
6 & 10: Unnecessary, use the "-p" option to include them w/ the read commands.
Also, you should list the allowable & or default responses in the prompt. See also B. & L.
7: `...` is deprecated, learn to use $(...)
7, 8, 14: It is safest to put the output of an echo in quotation marks -- double (quotes) if there is a variable name, single ones otherwise.
8: Good mention of "ctrl+d".
8: On my system (& probably most others) a regular user is not allowed to store a file in /root/.
11-13: This the major problem that needs fixing. You have both a syntax error on line 12; & a logic error which spans all 3 lines.
13-18: If you used case, instead of if...then...else, you could have made the structure (logic) easier to understand.
17: In real life, hardcoding like this is generally bad. Learn to use $0 instead, make any other changes necessary to make that work.
Note: read & case do not have their own man pages, you will have to use their CLI help (help <cmd>), or look them up in the bash man page which currently 4,924 lines
I am sorry, if my words are hurt anybody. Now onwards I never use such(LKG, u, i) words. I am sorry for that and thanks for the guidance.
Quote:
Originally Posted by archtoad6
Welcome to LQ, however late I am in saying it. I hope your time here helps you as much as mine has helped me.
Did you find a solution yet?
If so, please post it for others to benefit from.
What is an "LKG student in scrpting"?
Some pointers for using LQ effectively:
Please don't write chat/IM/leet style like "u" & "i", please take the time to write out "you" & "I". Your posts here are permanent & deserve more time & attention than in those media. TIA.
Please put code, command line output, config files, etc. inside [CODE] tags, aka "Code:" blocks.
It will make your posts easier to read, & that will get you more, faster, better answers. -- Help us help you.
BTW, You can edit your post(s) to do this retroactively. Please do so soon. If you need help doing this, please ask.
Thanks for you co-operation.
Is this in fact homework? Remember that we are happy to help guide to an answer, but not actually do it for you.
BTW, your script is a very good beginner's effort, & that makes the prospect of helping you more fun.
szboardstretcher's comment seems clear to me -- he's telling where to look for the logic & syntax errors that are keeping your script from working. More on this in my next post.
I have numbered my questions, please feel free to respond by number & not have to clutter your reply by quoting questions.
Hi..Thanks for the advise which could help me in finding the problems. I modified the shell script as below. I didn't use set -x since I don't know that how to use it. Could you please specify how to use -p option in read. I am using "" because to give space between one and another log. Is this script still has to be modified?
#!/bin/bash
echo "" >> /var/log/logbook && clear
echo What is your name?
read name && echo $name, $(date) > /tmp/logbook && clear
echo Write your administration work that will be stored in /var/log/logbook file, Use "ctrl+d" to terminate the session
cat >> /tmp/logbook
cat /tmp/logbook >> /var/log/logbook
echo Do you want write one more job? Type yes/no
read answer
if [ $answer != "yes" ]; then
echo These are your recent jobs, Bye!
cat /tmp/logbook
else
/home/ananth/test1.sh
fi
#END
Code:
1 #!/bin/bash
2 y=2
3 n=1
4 cd /root
5 echo "" >> logbook && clear
6 echo What is your name?
7 read name && echo $name, `date` > /tmp/logbook && clear
8 echo Write your administration work that will be stored in /root/logbook file, Use "ctrl+d" to terminate the session
9 cat >> /tmp/logbook && cat /tmp/logbook >> logbook
10 echo Do you want write one more log?
11 read answer
12 $read=1
13 if [ $read -eq $y ]; then
14 echo These are your recent jobs, Bye!
15 cat /tmp/logbook
16 else
17 /home/ananth/test1.sh
18 fi
19 #END
Most of what follows is minor, & included to try to help you improve. Hope it's helpful.
general: did you use set -x in debugging this?
2&3: These variables are unnecessary & the values you assign are non-standard.
4: You never cd back, this caused problems on my system, & may cause them where you would install this.
5: The '""' (double quotes) are unnecessary.
5, 7, 9: Good use of '&&'.
6 & 10: Unnecessary, use the "-p" option to include them w/ the read commands.
Also, you should list the allowable & or default responses in the prompt. See also B. & L.
7: `...` is deprecated, learn to use $(...)
7, 8, 14: It is safest to put the output of an echo in quotation marks -- double (quotes) if there is a variable name, single ones otherwise.
8: Good mention of "ctrl+d".
8: On my system (& probably most others) a regular user is not allowed to store a file in /root/.
11-13: This the major problem that needs fixing. You have both a syntax error on line 12; & a logic error which spans all 3 lines.
13-18: If you used case, instead of if...then...else, you could have made the structure (logic) easier to understand.
17: In real life, hardcoding like this is generally bad. Learn to use $0 instead, make any other changes necessary to make that work.
Note: read & case do not have their own man pages, you will have to use their CLI help (help <cmd>), or look them up in the bash man page which currently 4,924 lines[/QUOTE]
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.