I have a simple script that uses a for loop where I want to take the value of a variable and assign it to another command and kill the screen. The script goes as
#!/usr/bin/sh/
echo 'I am going to kill the screens now';
for j in $(seq 1 1 10)
do
i=$(grep -oP '.*?(?=\.)');
echo $i
screen -S $i -p 0 -X quit
done <file1.txt
echo 'exiting...'
file1.txt
There are screens on:
4974.eth2 (Detached)
5105.eth11 (Detached)
4990.eth3 (Detached)
5006.eth5 (Detached)
5054.eth8 (Detached)
5070.eth9 (Detached)
5038.eth7 (Detached)
5022.eth6 (Detached)
8 Sockets in /var/run/screen/S-root.
So here, I am iterating through 10 lines and assigning the value of i=whatever before a period(.). eg. 4974
. Then placing 4974
in screen command and killing it. The problem here is, it is reading and grepping all the lines at a time and in the value of i
it places as 4974 5105 4990 5006 5054 5070 5038 5022
in the screen command and it obviously it cannot find a screen id like that and doesn't kill any screen. How do i iterate the loop such that at a time it takes one id ie.value of i=4974
and place only that in the screen command so that it will kill it.. Let me know if you understand my question correctly.
Try this:
awk -F'[. ]+' '/Detached/ {print $2}' file.txt | while read i
do
echo "$i"
screen -S "$i" -p 0 -X quit
done
Instead of iterating over a counter, iterate over your results directly:
set -f # disable globbing, so globs in grep's output aren't expanded
echo 'I am going to kill the screens now' >&2
for i in $(grep -oP '.*?(?=\.)'); do
echo "$i"
screen -S "$i" -p 0 -X quit
done <file1.txt
echo 'exiting...' >&2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.