Read in bash script and passing argument to script -
i have question. unfortunately, didn't find answer. how can pass arguments script result of command. example:
ls | ./myscript.sh i want pass result of ls myscript. , if execute above command , script :
#!/bin/bash read arg in $@ grep $some $arg done then didn't wait read some variable , variable some gets value result passed ls command? how works ? want following: put script names of files (with command ls), after user enter string , want print every line contain entered word. didn't know why code above didn't work. in advance.
xargs utility use converting stdin input command-line arguments.
naively, following:
ls | xargs ./myscript.sh however, not work expected filenames have embedded spaces, split multiple arguments.
note ./myscript.sh $(ls) has same problem.
if xargs implementation supports nonstandard -0 option parsing nul-separated input, can fix follows:
printf '%s\0' * | xargs -0 ./myscript.sh otherwise, use following, which, however, work if no filenames have embedded " characters (or embedded newlines):
printf '"%s" ' * | xargs ./myscript.sh since stdin input comes filenames in case, can use find, has xargs functionality built in:
find . -type f -maxdepth 1 -exec ./myscript.sh {} + - note
find . -type f -maxdepth 1in essence samels, there subtle differences, notably each matching filename prefixed./, filenames may not sorted. -exec ./myscript.sh {} +invokes script many filenames ({}) can fit on single command line (+) -xargs- typically all of them.
note both xargs , find ... -exec ... + could result in multiple invocations of specified command, if not arguments fit on single command line.
however, given how long command lines allowed on modern platforms, happen - happens if have huge number of files in directory and/or names long.
Comments
Post a Comment