Linux Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsOperating SystemsLinux Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 24th, 2012, 12:08 PM
omgjtt omgjtt is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2010
Posts: 35 omgjtt User rank is Sergeant (500 - 2000 Reputation Level)omgjtt User rank is Sergeant (500 - 2000 Reputation Level)omgjtt User rank is Sergeant (500 - 2000 Reputation Level)omgjtt User rank is Sergeant (500 - 2000 Reputation Level)omgjtt User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 10 h 5 m 37 sec
Reputation Power: 13
A simple question with a bash script loop

I am new to bash and created a simple script to test for root user. Code below

Code:
if ['whoami' =  "root"] 
then 
     echo "You are the root user" 
else 
     echo "You are just a normal user" 
fi


This worked fine. I was then trying to get a little fancy and add an email notice. I created the following code

Code:
if ['whoami' =  "root"] 
then 
     echo "You are the root user" 
else 
     echo "an email has been sent to your email"
     mail -i -s "This is a test email" myemail@gmail.com 
fi


I then made the code executable on the command line with the following command chmod +x myfilename

I then did a listing with ls -l to confirm that the file was now executable. I am lost and would really appreciate a bit of help.

Thanks in advance,

James

Reply With Quote
  #2  
Old January 24th, 2012, 12:14 PM
omgjtt omgjtt is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2010
Posts: 35 omgjtt User rank is Sergeant (500 - 2000 Reputation Level)omgjtt User rank is Sergeant (500 - 2000 Reputation Level)omgjtt User rank is Sergeant (500 - 2000 Reputation Level)omgjtt User rank is Sergeant (500 - 2000 Reputation Level)omgjtt User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 10 h 5 m 37 sec
Reputation Power: 13
Sorry about the general post title. After reading the post I am not sure how simple it is since I have nothing to compare. I am brand new to this so I am assuming it is simple.

I should have named it "A simple bash question - I think"

Reply With Quote
  #3  
Old January 24th, 2012, 02:06 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,361 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 10 h 11 sec
Reputation Power: 383
What is your question?

I don't know what is your question.

[ is a synonym for test , a program.

You need a space between the [ and your whoami invocation. The back-tick style is deprecated. The test program ends its option list with ] . bash requires white space before ] to make it a separate argument. Of course, these could be copying/retyping problems that you didn't actually have when you ran the commands at your terminal.

Now you have an executable program. Since . (current directory) probably isn't on your PATH you'll need to provide the path to invoke your command. If it is in the same directory, use ./mycommand.sh . Or provide the full path name. Or some relative path name. Whatever. bash won't search the entire file system to find your command. You have to tell bash where it is.

Code:
if [ $( whoami ) =  "root" ]
then 
     echo "You are the root user" 
else 
     echo "You are just a normal user" 
fi
Comments on this post
omgjtt agrees!

Last edited by b49P23TIvg : January 24th, 2012 at 02:09 PM.

Reply With Quote
  #4  
Old January 24th, 2012, 02:14 PM
omgjtt omgjtt is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2010
Posts: 35 omgjtt User rank is Sergeant (500 - 2000 Reputation Level)omgjtt User rank is Sergeant (500 - 2000 Reputation Level)omgjtt User rank is Sergeant (500 - 2000 Reputation Level)omgjtt User rank is Sergeant (500 - 2000 Reputation Level)omgjtt User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 10 h 5 m 37 sec
Reputation Power: 13
Thanks b49P23TIvg

My question is - how can I add two commands after the else block.

I want it to echo "An email will be sent"
I then want it to send an email after the echo.

Thanks for the tip about the backtip being deprecated. I did not know this.

I hope this is clears up the question. Thanks

Reply With Quote
  #5  
Old January 24th, 2012, 03:08 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,361 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 10 h 11 sec
Reputation Power: 383
Your mail command must cause problems.
Change the mail command in your example to something fun like
yes more lines than you can count|head -6
and you'll see that bash evaluates both lines.

I haven't figured out how to set up mail on my system. The mailx man page says that -s sets the subject, and -i means "ignore keyboard interrupt". I presume your symptom is ``when I run the command the shell "just hangs"''.

Which likely means that mail patiently reads stdin to end of file. You'd enter the body of the message and end the file with C-d (^D) (control-d) .

If you already know the message body, change the input source and mail won't wait for you to type it.

Some ideas---

I'll bet you already have the message in a file! Then the command is (assuming -i is important which it probably isn't)

Code:
mail -i -s "noteworthy message" $( email_address $USER ) < message.body.txt


You'd precede the mail command with a program that customizes message.body.txt as appropriate. And you'd have to write or find the email_address program I just proposed.

If things are simpler, maybe you want to remind someone of the groups they belong to, the command would be

Code:
groups $USER | mail -s "Your groups on $( hostname )" $EMAIL


where $EMAIL expands to the correct email address. Good luck with that, I've never seen an EMAIL shell variable.
Comments on this post
omgjtt agrees!

Reply With Quote
  #6  
Old January 24th, 2012, 03:45 PM
omgjtt omgjtt is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2010
Posts: 35 omgjtt User rank is Sergeant (500 - 2000 Reputation Level)omgjtt User rank is Sergeant (500 - 2000 Reputation Level)omgjtt User rank is Sergeant (500 - 2000 Reputation Level)omgjtt User rank is Sergeant (500 - 2000 Reputation Level)omgjtt User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 10 h 5 m 37 sec
Reputation Power: 13
Thanks b49P23TIvg,

I really really am thankful for your input. I am heading home and will text out your suggestions there.

Thanks again

Reply With Quote
  #7  
Old January 26th, 2012, 12:32 PM
omgjtt omgjtt is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2010
Posts: 35 omgjtt User rank is Sergeant (500 - 2000 Reputation Level)omgjtt User rank is Sergeant (500 - 2000 Reputation Level)omgjtt User rank is Sergeant (500 - 2000 Reputation Level)omgjtt User rank is Sergeant (500 - 2000 Reputation Level)omgjtt User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 10 h 5 m 37 sec
Reputation Power: 13
Just a quick update -

I ended up using:
Code:
mail -i -s "noteworthy message" name@email.com < message_body.txt

It worked and did not hang up the command line waiting for body text. I hope this helps others dealing with the same issue.

Thank you very much.

Last edited by omgjtt : January 26th, 2012 at 04:08 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsLinux Help > A simple question with a bash script loop

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap