#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2010
    Posts
    35
    Rep Power
    15

    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
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2010
    Posts
    35
    Rep Power
    15
    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"
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,855
    Rep Power
    481

    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.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2010
    Posts
    35
    Rep Power
    15
    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
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,855
    Rep Power
    481
    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
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2010
    Posts
    35
    Rep Power
    15
    Thanks b49P23TIvg,

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

    Thanks again
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2010
    Posts
    35
    Rep Power
    15
    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.

IMN logo majestic logo threadwatch logo seochat tools logo