简体   繁体   中英

bash how to get var from file with multiple lines

I have two files, the first contains a series of names :

CONTAINER1
CONTAINER2
CONTAINER3
CONTAINER4

And the second script runs when the sintaxis is ./script.sh CONTAINER1

 CONT=$1
 PID=`opmnctl status -noheaders -fmt %prt30%pid7R  | grep -w $CONT | awk '{print $3}'`

 netstat -anp | grep $PID | grep ESTABLISHED > $CONT-temp

Is it possible to run the second script by parsing each line of the first file?. I'm new at bash, any help would be appreciated.

Thanks in advance

You can use xargs for this: it reads words from its stdin and appends them to the given command. Normally it takes several parameters at a time so it invokes the given command as few times as possible, but you can control that with the -L option:

xargs -L 1 echo "name is:" < containers.txt

produces

name is: CONTAINER1
name is: CONTAINER2
name is: CONTAINER3
name is: CONTAINER4

You would write

xargs -L 1 ./script.sh < containers.txt

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM