LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 08-25-2011, 10:37 AM   #1
loveulinux
Member
 
Registered: Aug 2011
Posts: 68

Rep: Reputation: Disabled
shell script to answer our questions


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.
Old 08-25-2011, 10:40 AM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,185

Rep: Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587
Well, I don't want to give you the exact answer to your homework, but here is where the problem lies:

Code:
read answer
$read=1
if [ $read -eq $y ]; then
If you look at this closely, you'll be able to figure out what is wrong.
 
2 members found this post helpful.
Old 08-26-2011, 11:16 AM   #3
loveulinux
Member
 
Registered: Aug 2011
Posts: 68

Original Poster
Rep: Reputation: Disabled
shell script to answer our questions

Sir, I am a LKG student in scrpting, So I can not understand and I request u to explain!

Quote:
Originally Posted by szboardstretcher View Post
Well, I don't want to give you the exact answer to your homework, but here is where the problem lies:

Code:
read answer
$read=1
if [ $read -eq $y ]; then
If you look at this closely, you'll be able to figure out what is wrong.
 
Old 08-26-2011, 11:41 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,498

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Well as you are a student, why don't you explain what you think those 3 lines are doing?
 
3 members found this post helpful.
Old 08-28-2011, 08:14 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,357

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
Try inserting
Code:
set -xv
as the 2nd line of your script; it'll show you exactly what bash is doing ...

Here's some good bash tutorials/refs
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
1 members found this post helpful.
Old 09-22-2011, 03:15 PM   #6
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
  1. Welcome to LQ, however late I am in saying it. I hope your time here helps you as much as mine has helped me.
  2. Did you find a solution yet?
  3. If so, please post it for others to benefit from.
  4. What is an "LKG student in scrpting"?
  5. Some pointers for using LQ effectively:
    1. 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.

    2. 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.

    3. Thanks for you co-operation.
  6. Is this in fact homework? Remember that we are happy to help guide to an answer, but not actually do it for you.
  7. BTW, your script is a very good beginner's effort, & that makes the prospect of helping you more fun.
  8. 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.
 
1 members found this post helpful.
Old 09-22-2011, 04:46 PM   #7
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
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.
  1. general: did you use set -x in debugging this?
  2. 2&3: These variables are unnecessary & the values you assign are non-standard.
  3. 4: You never cd back, this caused problems on my system, & may cause them where you would install this.
  4. 5: The '""' (double quotes) are unnecessary.
  5. 5, 7, 9: Good use of '&&'.
  6. 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. 7: `...` is deprecated, learn to use $(...)
  8. 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.
  9. 8: Good mention of "ctrl+d".
  10. 8: On my system (& probably most others) a regular user is not allowed to store a file in /root/.
  11. 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.
  12. 13-18: If you used case, instead of if...then...else, you could have made the structure (logic) easier to understand.
  13. 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
 
Old 09-23-2011, 09:57 PM   #8
loveulinux
Member
 
Registered: Aug 2011
Posts: 68

Original Poster
Rep: Reputation: Disabled
shell script to answer our questions

Thanks for providing bash url links.

Quote:
Originally Posted by chrism01 View Post
Try inserting
Code:
set -xv
as the 2nd line of your script; it'll show you exactly what bash is doing ...

Here's some good bash tutorials/refs
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
Old 09-23-2011, 10:12 PM   #9
loveulinux
Member
 
Registered: Aug 2011
Posts: 68

Original Poster
Rep: Reputation: Disabled
shell script to answer our questions

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 View Post
  1. Welcome to LQ, however late I am in saying it. I hope your time here helps you as much as mine has helped me.
  2. Did you find a solution yet?
  3. If so, please post it for others to benefit from.
  4. What is an "LKG student in scrpting"?
  5. Some pointers for using LQ effectively:
    1. 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.

    2. 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.

    3. Thanks for you co-operation.
  6. Is this in fact homework? Remember that we are happy to help guide to an answer, but not actually do it for you.
  7. BTW, your script is a very good beginner's effort, & that makes the prospect of helping you more fun.
  8. 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.
 
Old 09-23-2011, 10:36 PM   #10
loveulinux
Member
 
Registered: Aug 2011
Posts: 68

Original Poster
Rep: Reputation: Disabled
shell script to answer our 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.
  1. general: did you use set -x in debugging this?
  2. 2&3: These variables are unnecessary & the values you assign are non-standard.
  3. 4: You never cd back, this caused problems on my system, & may cause them where you would install this.
  4. 5: The '""' (double quotes) are unnecessary.
  5. 5, 7, 9: Good use of '&&'.
  6. 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. 7: `...` is deprecated, learn to use $(...)
  8. 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.
  9. 8: Good mention of "ctrl+d".
  10. 8: On my system (& probably most others) a regular user is not allowed to store a file in /root/.
  11. 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.
  12. 13-18: If you used case, instead of if...then...else, you could have made the structure (logic) easier to understand.
  13. 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]
 
Old 09-24-2011, 03:52 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,498

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Quote:
I didn't use set -x since I don't know that how to use it.
Place it in your script directly after the first line:
Code:
#!/bin/bash
set -x
Quote:
Could you please specify how to use -p option in read.
In your terminal type the following:
Code:
help read
Before we read your script, please place it in [code][/code] tags so it is legible, ie it will look like what you copied from archtoad6.
 
  


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] script help (Case & IF statement syntax), easy to answer if you know shell scripting. casperpache Linux - Newbie 12 05-09-2011 07:18 AM
How To Answer System Questions Inside A Bash Script ? zemon Programming 4 01-24-2011 06:07 PM
three shell script questions qrshat Programming 4 02-27-2006 08:40 AM
two shell script questions tutwabee Linux - General 4 08-10-2004 06:03 PM
Shell Script Questions Col Panic Linux - General 13 10-12-2003 12:51 PM


All times are GMT -5. The time now is 12:42 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration