I'd like to know how to use the contents of a file as command line arguments, but am struggling with syntax.
Say I've got the following:
# cat > arglist
src/file1 dst/file1
src/file2 dst/file2
src/file3 dst/file3
How can I use the contents of each line in the arglist
file as arguments to say, a cp
command?
the '-n' option for xargs specifies how many arguments to use per command :
$ xargs -n2 < arglist echo cp
cp src/file1 dst/file1
cp src/file2 dst/file2
cp src/file3 dst/file3
Using read
(this does assume that any spaces in the filenames in arglist
are escaped):
while read src dst; do cp "$src" "$dst"; done < argslist
If the arguments in the file are in the right order and filenames with spaces are quoted, then this will also work:
while read args; do cp $args; done < argslist
You can use pipe (|) :
cat file | echo
or input redirection (<)
cat < file
or xargs
xargs sh -c 'emacs "$@" < /dev/tty' emacs
Then you may use awk to get arguments:
cat file | awk '{ print $1; }'
Hope this helps..
如果您的目的只是 cp 列表中的那些文件
$ awk '{cmd="cp "$0;system(cmd)}' file
Use for
loop with IFS
(Internal Field Separator) set to new line
OldIFS=$IFS # Save IFS
$IFS=$'\n' # Set IFS to new line
for EachLine in `cat arglist"`; do
Command="cp $Each"
`$Command`;
done
IFS=$OldIFS
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.