Correct Syntax for bash script

General support questions
Post Reply
kap5ul
Posts: 3
Joined: 2021/03/30 09:30:17

Correct Syntax for bash script

Post by kap5ul » 2021/03/30 10:18:46

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

MartinR
Posts: 714
Joined: 2015/05/11 07:53:27
Location: UK

Re: Correct Syntax for bash script

Post by MartinR » 2021/03/30 11:32:25

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:

Code: Select all

ping -c5 $switch
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:

Code: Select all

#!/bin/bash -vx
which will cause each line to be displayed and the substitutions shown. Obviously remove the -vx for serious work.
HTH

User avatar
TrevorH
Site Admin
Posts: 33202
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

Re: Correct Syntax for bash script

Post by TrevorH » 2021/03/30 14:16:25

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

scottro
Forum Moderator
Posts: 2556
Joined: 2007/09/03 21:18:09
Location: NYC
Contact:

Re: Correct Syntax for bash script

Post by scottro » 2021/03/30 18:50:40

For going through a list like that, I prefer while to for.

Code: Select all

while read line
do echo "$line"  >>result.txt
ping -c 1 "$line" >> result.txt
done < switches.txt
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.)
New users should check the FAQ and Read Me First pages

kap5ul
Posts: 3
Joined: 2021/03/30 09:30:17

Re: Correct Syntax for bash script

Post by kap5ul » 2021/03/31 09:31:35

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

sml
Posts: 305
Joined: 2020/01/17 09:01:44

Re: Correct Syntax for bash script

Post by sml » 2021/03/31 14:20:20

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.

kap5ul
Posts: 3
Joined: 2021/03/30 09:30:17

Re: Correct Syntax for bash script

Post by kap5ul » 2021/04/01 06:32:40

thanks, it seems to be good.
Sometimes, it's just a simple things that you're not thinking about..

Thanks all for helping

pjsr2
Posts: 614
Joined: 2014/03/27 20:11:07

Re: Correct Syntax for bash script

Post by pjsr2 » 2021/04/01 07:09:36

To convert files with Windows style line-endings to Unix style you can use the command

Code: Select all

dos2unix filename
Case it is not installed:

Code: Select all

sudo yum install dos2unix
The package contains dos2unix, unix2dos, mac2unix and unix2mac.

Post Reply