SSF
SSF

Reputation: 983

Using while/read/do to pass the content of file as the argument of a command

I'm really new to Linux scripting. I am sure this is simple, but I cannot figure it out. As part of a script, I am trying to pass the content of a file as arguments of a command in a script:

while read i 

do $COMMAND $i 

done < file.lst

I want to pass every line of the file.lst as the argument of the command except the very first line of the file. How to I do this?

EDIT:

Here is the section of the script:

while read i 
do cp --recursive --preserve=all $i $DIR
done < $DIR/file.lst

Upvotes: 0

Views: 681

Answers (3)

chepner
chepner

Reputation: 530853

Add an extra read to consume the first line before the while loop begins.

{
    read -r;
    while read -r i; do
        "$COMMAND" "$i"
    done
} < file.lst

Upvotes: 0

Cyrus
Cyrus

Reputation: 88553

while read -r i
do
  "$COMMAND" "$i"
done < <(sed -n '2,$p' file.lst)

Upvotes: 1

Leon
Leon

Reputation: 12481

This solutions does not use a while so I am not entirely sure if it solves your problem, but based on your code sample. you can do the following

tail -n +2 input | xargs echo

This will read all lines from input starting at line 2 and execute echo using the value of the line

the file input contains:

skip
1
2
3

executing that command gives

1
2
3

Just substitute input for the file you want and echo for the command you want

Upvotes: 0

Related Questions