Bash loop never ends - bash

I wonder why this script never ends?
#! /bin/bash
for ip in `seq 200 254`; do
ping -c 1 192.168.0.$ip | grep "bytes from" | cut -d " " -f 4 | cut -d ":" -f 1 &
done
But if I choose not to run commands in parallel, script ends up just ok.
#! /bin/bash
for ip in `seq 1 254`; do
ping -c 1 192.168.0.$ip | grep "bytes from" | cut -d " " -f 4 | cut -d ":" -f 1
done
Could somebody explain, please, why is it happens?

Your for loops are doing the exact same thing. The only difference is that in the first instance you are running all commands in the background and will not see a termination. Specifically:
ping -c 1 192.168.0.$ip | grep "bytes from" | cut -d " " -f 4 | cut -d ":" -f 1 &
^^^
Using the & operator at the end tells bash to background the process. Eliminating the & runs the process in the foreground where the execution is visible.

Related

seq command unkown service or name

#!/bin/bash
for ip in 'seq 1 254'; do
ping -c 1 $0.$ip | grep "64 bytes" | cut -d " " -f 4 | tr -d ":" &
done
the file is ipsweep.sh
when i run it show me this
root#kali:ping: ./ipsweep.sh.seq: Name or service unkown
First issue:
Replace 'seq 1 254' with $(seq 1 254).
If you replace both ' with backticks it works too, but it's old syntax.
Second issue:
Replace $0 with $1 if you want to provide this part (192.168.1, e.g.) on the command line. $0 contains name of your script.

How to disable error message sent by command "ping"?

I wrote a simple ping sweeper using bash script. I also use grep command to filter out the result I want. The problem is, the console keep printing out error message: "ping: recvmsg: No route to host" no matter what grep command I tried. I tried to write the output into a file, and there is no error message inside the file but they still appear on the console. I want to know what causes the console to print out error message like that and how to disable it, thanks.
Here is the script I wrote.
#!/bin/bash
for ip in $(seq 1 254); do
#ping -c 1 10.11.1.$ip | grep -v "recvmsg" | grep "bytes from" | cut -d " " -f 4 | cut -d ":" -f 1 &
ping -c 1 10.11.1.$ip | grep -v "recvmsg" |grep -v "ping" | grep "bytes from" | cut -d " " -f 4 | cut -d ":" -f 1| sort -d >> report &
done
wait
And here is the error message
ping: recvmsg: No route to host
You can use the redirectors for stderr (standard error) you only need put this at the end of your command 2> error.log
#!/bin/bash
for ip in $(seq 1 254); do
#ping -c 1 10.11.1.$ip | grep -v "recvmsg" | grep "bytes from" | cut -d " " -f 4 | cut -d ":" -f 1 &
ping -c 1 10.11.1.$ip 2> error.log | grep -v "recvmsg" |grep -v "ping" | grep "bytes from" | cut -d " " -f 4 | cut -d ":" -f 1| sort -d >> report &
done
wait

I'm trying to make an until loop stop once I get a result

#!/bin/bash
cd /root/.OceanVieW
keyword=$( cat oceanview.conf |grep "seaworld" |cut -d "=" -f2);
until [ ${#keyword} -gt 1 ] ; do sleep 30
cat oceanview.conf |grep "seaworld" |cut -d "=" -f2 ;
done
The loop doesn't stop, it keeps going on. I want it to stop once I get a result.
Simplification, (as per OP comment), loop until seaworld=0 is not found:
until grep -m 1 -vq '^seaworld=0$' oceanview.conf ; do
sleep 30
done
If verbose output is needed, (a running list of numbers, which would all be 0 except the last one tested), try:
until grep -m 1 -v '^seaworld=0$' oceanview.conf ; do
sleep 30
done | cut -d "=" -f2
If that's too verbose, then right after the -f2 append | uniq -c, to show a count of 0s, and the final number, printed after the loop completes.

2nd echo statement in bash script not working

For some reason I'm not getting the output of my 2nd echo line when I run my script. Here is my code snippet:
IS_RUNNING=$(netstat -anp | grep ":7600" | grep java | awk '{print $7}' | cut -d"/" -f 2)
start(){
nohup /bin/su -c "/opt/app/bin/service start" - user &>/dev/null &
echo "Starting Services please wait"
sleep 30
if [ "$IS_RUNNING" = java ];
then
echo "Service is now running"
exit 0
fi
}
Interestingly. when I run it with:
sh -x ./service start
I get the output expected and my 2nd echo gets written to the screen.
+ case "$1" in
+ start
+ echo 'Starting Services please wait'
Starting Services please wait
+ sleep 30
+ nohup /bin/su -c '/opt/app/bin/service start' - user
+ '[' java = java ']'
+ echo 'Service is now running'
Service is now running
+ exit 0
Without using sh -x, I simply get this:
[root#init.d]# ./service start
Starting Services please wait
[root#init.d]#
I have a feeling I'm overlooking something simple. Can anyone help?
The running line is evaluated once:
IS_RUNNING=$(netstat -anp | grep ":7600" | grep java | awk '{print $7}' | cut -d"/" -f 2)
You should make a function for this or include this line in the start() function after the sleep.

Too many arguments error in shell script

I am trying a simple shell script like the following:
#!/bin/bash
up_cap=$( cat result.txt | cut -d ":" -f 6,7 | sort -n | cut -d " " -f 2 | sort -n)
down_cap=$( cat result.txt | cut -d : -f 6,7 | sort -n | cut -d " " -f 6| sort -n)
for value in "${down_cap[#]}";do
if [ $value > 80000 ]; then
cat result.txt | grep -B 1 "$value"
fi
done
echo " All done, exiting"
when I execute the above script as ./script.sh, I get the error:
./script.sh: line 5: [: too many arguments
All done, exiting
I have googled enough, and still not able to rectify this.
You want
if [ "$value" -gt 80000 ]; then
You use -gt for checking if A is bigger than B, not >. The quotation marks I merely added to prevent the script from failing in case $value is empty.
Try to declare variable $value explicitly:
declare -i value
So, with the dominikh's and mine additions the code should look like this:
#!/bin/bash
up_cap=$( cat result.txt | cut -d ":" -f 6,7 | sort -n | cut -d " " -f 2 | sort -n)
down_cap=$( cat result.txt | cut -d : -f 6,7 | sort -n | cut -d " " -f 6| sort -n)
for value in "${down_cap[#]}";do
declare -i value
if [ $value -gt 80000 ]; then
cat result.txt | grep -B 1 "$value"
fi
done
echo " All done, exiting"

Resources