Find the answer to your Linux question:
Results 1 to 3 of 3
Hey guys. This is my first post. I've found a lot of answers I need on this site in the past by googling. Here is my problem. I'm writing a ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2011
    Posts
    5

    Bash-If statement issues


    Hey guys. This is my first post. I've found a lot of answers I need on this site in the past by googling. Here is my problem. I'm writing a script for work (i work network security) and this script tests printers to make sure their default passwords have been changed. I'm trying to write an if statement based on if the address is ping able or not. My problem is if [ "ping -c1 -w1 $ip | grep 1 received" ] If the IP isn't ping able i want it to store that IP in a new file. I can't grep for "1 received" because there is a space. does anyone have a work around for this or can tell me what my problem is?

    ip=`sed -n "$x p" IPlist.txt`
    if [ "ping -c1 -w1 $ip | grep 1 received" ]
    then
    #code to test password
    line=$[$line-1]
    x=$[$x+1]
    else
    echo "IP is not responding"
    echo "$ip not online" >> offlinePrinters.txt
    line=$[$line-1]
    x=$[$x+1]
    fi

    Thanks in advanced
    (I havn't written bash in a loooong time)

  2. #2
    Just Joined!
    Join Date
    Oct 2011
    Posts
    5
    Oh and by the way. This doesn't throw any error messages. It just doesn't write "$ip not online" to "offlinePrinters.txt" so it runs... but it doesn't work

  3. #3
    Just Joined!
    Join Date
    Oct 2011
    Posts
    5
    Figured it out. I had to use ( ) around the condition. Not [ ]

Posting Permissions

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