Correct Syntax for bash script
Correct Syntax for bash script
hi
Can you help me to make this script in the correct syntax.
In this case the "echo" command work fine but not the "ping" command.
It's work all fine in a debian machine but not in my Centos-Centreon which i have to use to check all my switch with snmp.
The ping is for the example, in my real script, i will use snmp request to the switches, but my problem is that the variable is not use corectly by the command i want to use.
thanks for help.
#!/bin/bash
switches=$(cat switch.txt)
for switch in $switches
do
echo $switch
ping $switch
done
Can you help me to make this script in the correct syntax.
In this case the "echo" command work fine but not the "ping" command.
It's work all fine in a debian machine but not in my Centos-Centreon which i have to use to check all my switch with snmp.
The ping is for the example, in my real script, i will use snmp request to the switches, but my problem is that the variable is not use corectly by the command i want to use.
thanks for help.
#!/bin/bash
switches=$(cat switch.txt)
for switch in $switches
do
echo $switch
ping $switch
done
Re: Correct Syntax for bash script
The only problem that I see with the script is that the ping will continue for ever. You need to set the count to a value, for instance 5:
I did a copy/paste onto an unprivileged account and set up a three line switch.txt. Everything worked fine.
When sorting out problems like this it is often helpful to change the first line to:
which will cause each line to be displayed and the substitutions shown. Obviously remove the -vx for serious work.
HTH
Code: Select all
ping -c5 $switch
When sorting out problems like this it is often helpful to change the first line to:
Code: Select all
#!/bin/bash -vx
HTH
Re: Correct Syntax for bash script
What does ping error with?
The future appears to be RHEL or Debian. I think I'm going Debian.
Info for USB installs on http://wiki.centos.org/HowTos/InstallFromUSBkey
CentOS 5 and 6 are deadest, do not use them.
Use the FAQ Luke
Info for USB installs on http://wiki.centos.org/HowTos/InstallFromUSBkey
CentOS 5 and 6 are deadest, do not use them.
Use the FAQ Luke
Re: Correct Syntax for bash script
For going through a list like that, I prefer while to for.
That's pinging each switch once and writing the result to file result.txt
(You could further improve it with various text processing tools so that you get a result just printing whatever information you need.)
Code: Select all
while read line
do echo "$line" >>result.txt
ping -c 1 "$line" >> result.txt
done < switches.txt
(You could further improve it with various text processing tools so that you get a result just printing whatever information you need.)
New users should check the FAQ and Read Me First pages
Re: Correct Syntax for bash script
thanks for help,
there is the result for the script
#!/bin/bash -vx
while read line
do echo "$line" >>result.txt
ping -c 1 "$line" >> result.txt
done < switch.txt
+ read line
+ echo $'10.xx.xx.xx\r'
+ ping -c 1 $'10.xx.xx.xx\r'
: Nom ou service inconnu
+ read line
+ echo $'10.xx.xx.xx\r'
+ ping -c 1 $'10.xx.xx.xx7\r'
: Nom ou service inconnu
+ read line
I'vre replace the real IP by xx, it seems to me that the "\r" at the end of the variable may be the problem.
How can i do for remove it?
The file result.txt contains the good IP when the script as finished without "\r".
thanks
there is the result for the script
#!/bin/bash -vx
while read line
do echo "$line" >>result.txt
ping -c 1 "$line" >> result.txt
done < switch.txt
+ read line
+ echo $'10.xx.xx.xx\r'
+ ping -c 1 $'10.xx.xx.xx\r'
: Nom ou service inconnu
+ read line
+ echo $'10.xx.xx.xx\r'
+ ping -c 1 $'10.xx.xx.xx7\r'
: Nom ou service inconnu
+ read line
I'vre replace the real IP by xx, it seems to me that the "\r" at the end of the variable may be the problem.
How can i do for remove it?
The file result.txt contains the good IP when the script as finished without "\r".
thanks
Re: Correct Syntax for bash script
The file switch.txt was probably created on Windows. You can
1) permanently remove all CRs from the file, e.g. sed -i 's/\r$//' switch.txt, or
2) remove them on-the-fly while reading the file: done <(tr -d \\r <switch.txt), or
3) read line until CR only: while read -d$'\r' line, or
4) remove the last character from the line: ${line%?}, etc.
1) permanently remove all CRs from the file, e.g. sed -i 's/\r$//' switch.txt, or
2) remove them on-the-fly while reading the file: done <(tr -d \\r <switch.txt), or
3) read line until CR only: while read -d$'\r' line, or
4) remove the last character from the line: ${line%?}, etc.
Re: Correct Syntax for bash script
thanks, it seems to be good.
Sometimes, it's just a simple things that you're not thinking about..
Thanks all for helping
Sometimes, it's just a simple things that you're not thinking about..
Thanks all for helping
Re: Correct Syntax for bash script
To convert files with Windows style line-endings to Unix style you can use the command
Case it is not installed:
The package contains dos2unix, unix2dos, mac2unix and unix2mac.
Code: Select all
dos2unix filename
Code: Select all
sudo yum install dos2unix