Find the answer to your Linux question:
Results 1 to 9 of 9
I think I have the basic logic down, but I can't get the syntax right for the bash shell. It needs to read the /etc/passwd file and send an e-mail ...
  1. #1
    Just Joined!
    Join Date
    May 2007
    Posts
    26

    Unhappy syntax errors...please help me...

    I think I have the basic logic down, but I can't get the syntax right for the bash shell. It needs to read the /etc/passwd file and send an e-mail to all users not currently using a password.

    #!/bin/bash
    #Checking for file existence
    if test -f /etc/passwd ; then
    echo "The passwd file was found!"
    else
    echo "The passwd file could not be found!?"
    exit
    fi
    # display only lines with /bin/bash
    USRS=`cat /etc/passwd | egrep "/bin/bash" | awk -F: '$2 != "x" {print $1}'`
    echo ${USRS}
    USRS=${USRS}" X"

    EMAILMESSAGE="Your account does not have a password. Please create one immediately using the passwd command."
    SUBJECT="No Password"
    i=`expr 1`

    while [ 1 ]
    do
    USR=`awk {print $($i)} ${USRS}`
    if [${USR} = "X"] ; then
    exit
    else
    USR=${USR}"@mydomain.com"
    /bin/mail -s "${SUBJECT}" "${USR}" < $EMAILMESSAGE
    fi
    i=`expr $i + 1`
    done

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    What does the syntax error say? What line is it occurring on?

    A quick look over the script doesn't show me anything obvious, but until I know what I'm looking for, I'm not going to do anything more.
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by brewboy View Post
    I think I have the basic logic down, but I can't get the syntax right for the bash shell. It needs to read the /etc/passwd file and send an e-mail to all users not currently using a password.

    #!/bin/bash
    #Checking for file existence
    if test -f /etc/passwd ; then
    echo "The passwd file was found!"
    else
    echo "The passwd file could not be found!?"
    exit
    fi
    # display only lines with /bin/bash
    USRS=`cat /etc/passwd | egrep "/bin/bash" | awk -F: '$2 != "x" {print $1}'`
    echo ${USRS}
    USRS=${USRS}" X"

    EMAILMESSAGE="Your account does not have a password. Please create one immediately using the passwd command."
    SUBJECT="No Password"
    i=`expr 1`

    while [ 1 ]
    do
    USR=`awk {print $($i)} ${USRS}`
    if [${USR} = "X"] ; then
    exit
    else
    USR=${USR}"@mydomain.com"
    /bin/mail -s "${SUBJECT}" "${USR}" < $EMAILMESSAGE
    fi
    i=`expr $i + 1`
    done
    in bash, you can use i=$(($i+1)) to increment variable. Your while loop loops forever (if you don't have a user called "X"). you can use for loop , looping over the USR variable : eg
    Code:
    for usr in $USR
    lastly, at least show what your error is when you run this code.

  4. #4
    Just Joined!
    Join Date
    May 2007
    Posts
    26

    Smile Thanks that helps!

    Sorry about not posting the error, I've never really used forums before. Here's the error I'm getting:

    "Who are you?: Permission denied"... and then it loops forever.

    I'll try the for loop and the incrementation, that's definitely what I've been looking for.

    Thanks again

  5. #5
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    I don't recognize the error, but it sounds like the call to 'mail' might be throwing it. Are you sure that you're invoking it correctly?
    DISTRO=Arch
    Registered Linux User #388732

  6. #6
    Just Joined!
    Join Date
    May 2007
    Posts
    26

    No I'm not sure...

    I'm very new to linux. I open up man pages and they still look a lot like a wall of text. Could you recommend some ways to do so...invoke mail that is?

  7. #7
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Out of curiosity, is the e-mail being sent anyway? One of the few references I can find online mentions that he gets this error, but the e-mail is still sent.

    Also, what are the permissions on your /etc/passwd file? You can determine this by running the command:
    Code:
    ls -l /etc/passwd
    DISTRO=Arch
    Registered Linux User #388732

  8. #8
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    Excerpt from original script:
    Code:
     if [${USR} = "X"] ; then
    exit
    else
    USR=${USR}"@mydomain.com"
    /bin/mail -s "${SUBJECT}" "${USR}" < $EMAILMESSAGE
    I would change the if to use required spaces around the square brackets:
    Code:
    if [ ${USR} = "X" ] ; then
    and the mail line will attempt to read the mail message from a file named:
    Code:
    Your account does not have a password. Please ...
    because you are requesting to read from a file name rather than using the text of the variable. You can make an easy change by using a here document. I know this can be confusing when you are learning scripting.

    One way to make this work is to use a test -- use cat instead of /bin/mail:
    Code:
    cat <<EOF
    $EMAILMESSAGE
    EOF
    If you can see the message, then replace the cat with /bin/mail and other parameters, but be sure to keep the here document.

    Also as a test, arrange to send the email to yourself, so that you can see if things are working correctly. When you start getting the email, then you can start using the other addresses.

    Please try to get in the habit of using CODE tags, it will make it easier for responders to help you -- select the text and then click the # button just above the editing window,

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  9. #9
    Just Joined!
    Join Date
    May 2007
    Posts
    26

    Smile Thank You!

    I was able to get it working, and I was making it overly complicated. Thank you for the help with the syntax, that was one of the major issues. Also, the e-mails were getting sent. The error was about a user that could sign in, but the system didn't recognize them as having a user name... I don't really get that, but I fixed it. All is well and thanks to everyones help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...