LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 12-16-2014, 12:00 PM   #1
Billy_6052
LQ Newbie
 
Registered: Dec 2014
Posts: 14

Rep: Reputation: Disabled
bash script: while loop failing


hi

can anyone see anything wrong with my script??

#!/bin/bash
while true
do
PS3='Please select the log file to view: '
options=("system log" "boot log" "Quit")
select opt in "${options[@]}"
do
case $opt in
"system log")
cat /var/log/messages | grep --color=always "error" > errlog.txt
break
;;
"boot log")
cat /var/log/messages | grep --color=always "error > booterr.txt
break
;;

"Quit")
echo "Thank You..."
exit
;;
esac
done
done

I want to give options whether to see the system log or boot logs but nothings working. please help
 
Old 12-16-2014, 12:15 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
For starters, place another double quote on line 19 between error and > booterr.txt because you are missing an endquote on that line.

Not all systems have /var/log/messages, the output of dmesg sometimes is better, but I'm no expert on the system logs.
 
1 members found this post helpful.
Old 12-16-2014, 12:29 PM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Just some additional comments:
do not use cat filename | grep ...., but grep .... filename (cat and pipe is not required at all)
use an editor with syntax highlight and you will see immediately if something is wrong (at least in most cases)
use [code]here comes your script[/code] to keep formatting of your post
 
1 members found this post helpful.
Old 12-16-2014, 12:32 PM   #4
Billy_6052
LQ Newbie
 
Registered: Dec 2014
Posts: 14

Original Poster
Rep: Reputation: Disabled
hi rtmistler!

thank you soo much for the fast reply! I have corrected the mistakes however when I run my script I get a recursive

myscript.sh: line 5: please select the log file you want: command not found


myscript.sh: line 5: please select the log file you want: command not found

myscript.sh: line 5: please select the log file you want: command not found

myscript.sh: line 5: please select the log file you want: command not found

myscript.sh: line 5: please select the log file you want: command not found

myscript.sh: line 5: please select the log file you want: command not found

and to exit out I have to cntrl c. I just want to have a menu, and there is a messages on the system im running. any ideas?
 
Old 12-16-2014, 12:39 PM   #5
Billy_6052
LQ Newbie
 
Registered: Dec 2014
Posts: 14

Original Poster
Rep: Reputation: Disabled
hi pan64

thank you so much for your reply!!

I put code and code and the beginning and end of the script and now getting:

myscript.sh: line 6: please select the log file you want: command not found
myscript.sh: line 6: please select the log file you want: command not found
myscript.sh: line 6: please select the log file you want: command not found
myscript.sh: line 6: please select the log file you want: command not found
myscript.sh: line 6: please select the log file you want: command not found

this is so confusing lol

I just want to have a menu system so I am able to view any logs I want to

any ideas? thanks again
 
Old 12-16-2014, 12:39 PM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
You may have an extra line there, the absolute first line should be your bang line:
Code:
#!/bin/bash
Line #5 I have would be:
Code:
options=("system log" "boot log" "Quit")
And it appears that line #5 in your error situation is one above that:
Code:
PS3='Please select the log file to view: '
My only guesses there are:
  1. You have an extra line before the #!/bin/bash line
  2. You've added/edited beyond your original post, because the copied error has a lowercase 'p' for "Please" whereas your original posting has that as uppercase.

See below about posting your code with [CODE] tags.

Make sure the #!/bin/bash is your absolute first physical line of the script.

The complaint appears to be taking your "please select" text as a command, which means the shell parser is all out of alignment with your coded intentions.

The script worked when I cut/pasted it in the original form, with the added quotation I mentioned.
 
1 members found this post helpful.
Old 12-16-2014, 12:42 PM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by Billy_6052 View Post
hi pan64

thank you so much for your reply!!

I put code and code and the beginning and end of the script and now getting:

myscript.sh: line 6: please select the log file you want: command not found
myscript.sh: line 6: please select the log file you want: command not found
myscript.sh: line 6: please select the log file you want: command not found
myscript.sh: line 6: please select the log file you want: command not found
myscript.sh: line 6: please select the log file you want: command not found

this is so confusing lol

I just want to have a menu system so I am able to view any logs I want to

any ideas? thanks again
No, no, no, no, no ...


The [code] framers are only used to post code to a post in this forum. As I've said, follow the link in my signature showing how to add those tags to your posts.

pan's comment was not for your script. See my last suggestion.
 
1 members found this post helpful.
Old 12-16-2014, 12:44 PM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
yes, you need to put set -xv at the very beginning of the script (second line) - just to see what's happening.
And you will see select makes a loop itself, your break will quit that loop, but not the while loop. Also you modified the script and I have no idea what was running.
 
1 members found this post helpful.
Old 12-16-2014, 12:51 PM   #9
Billy_6052
LQ Newbie
 
Registered: Dec 2014
Posts: 14

Original Poster
Rep: Reputation: Disabled
THAAAAANNNKKKS GUYS I GOT IT WORKING! i just copied/pasted my original script. however when i select an option it shows the 3 options again? if you know what i mean? i would of thought if i selected boot log that it would write to the booterr.txt and just exit
 
Old 12-16-2014, 12:53 PM   #10
Billy_6052
LQ Newbie
 
Registered: Dec 2014
Posts: 14

Original Poster
Rep: Reputation: Disabled
Never mind guys i got it working!!!!!!!!!!!!!! Woooooooooooooooooooooooooooooooooo thank you so muchhhh
 
Old 12-16-2014, 12:57 PM   #11
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by Billy_6052 View Post
THAAAAANNNKKKS GUYS I GOT IT WORKING! i just copied/pasted my original script. however when i select an option it shows the 3 options again? if you know what i mean? i would of thought if i selected boot log that it would write to the booterr.txt and just exit
That's because in options 1 or 2 you just break your switch statement and the loop continues. Only option #3 exits. If you wish to exit, then you can add exit to any or all cases.

I think you're getting the gist. Check my blogs, here's one with some of my takes on bash scripting, debugging it, and other suggestions, BASH Scripting for Dummies and Geniuses

Seems like you're experimenting with BASH scripts and this is one of your experiments. Good job. I say keep posting some further works and questions or if you're seeking comments, lots of people usually reply to script questions.
 
1 members found this post helpful.
Old 12-16-2014, 12:58 PM   #12
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Exciting, isn't it?
 
1 members found this post helpful.
Old 12-16-2014, 01:03 PM   #13
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Not that some of the other common guru's, Moderators, or others aren't also very adept. Just the username danielbmartin in LQ comes up a lot. They play a lot with scripts, post them, and also post for script questions and such. They seem to be working just for their own intellect and technical entertainment (I believe they mentioned that in one post), but if you search for posts by them, you'll see a lot of scripts as well as variations that many others have offered. Or just search the forums in general for BASH SCRIPT, believe me, you'll find a ton of forum posts. Lots of times you can just cut/paste many of the scripts and try them out.
 
1 members found this post helpful.
Old 12-16-2014, 01:34 PM   #14
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
https://www.google.com/?gws_rd=ssl#q...xquestions.org
 
  


Reply



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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
Working Expect script failing when placed inside bash Loop. Sigmaflux Linux - Newbie 1 01-27-2014 10:51 PM
Bash script issue (for loop inside a loop) Mperonen Programming 3 08-08-2013 02:14 AM
mount -o loop command failing in startup script for RH 5 dougkdp76 Linux - General 3 01-12-2008 11:04 AM
bash script for loop drisay Programming 5 12-25-2004 12:32 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 03:35 PM.

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