linux - Multiple variables in loop input? -
when using following:
for in $@; read -p "where?" grep -h "$what" $where -r | cutting -d: -f1
how can i, instead of using read define user-variable, have sec variable input along first variable when calling script.
for example, ideal usage believe can like:
sh scriptname var1 var2
but understanding for... line looping subsequent entires 1 variable; need alter input multiple variables?
as aside: using | cutting -d: -f1
not safe, because grep not escape colons in filenames. see mean, can seek this:
ghoti@pc:~$ echo bar:baz > foo ghoti@pc:~$ echo baz > foo:bar ghoti@pc:~$ grep -hr ba . ./foo:bar:baz ./foo:bar:baz
clarity .. there not.
so ... let's clarify you're looking for.
do want search 1 string in multiple files? or, do want search multiple strings in 1 file?if former, next might work:
#!/bin/bash if [[ "$#" -lt 2 ]]; echo "usage: `basename $0` string file [file ...] exit 1 fi what="$1" shift # discard $1, move $2 $1, $3 $2, etc. in "$@"; grep -hlr "$what" "$where" -r done
and if latter, way:
#!/bin/bash if [[ "$#" -lt 2 ]]; echo "usage: `basename $0` file string [string ...] exit 1 fi where="$1" shift in "$@"; grep -lr "$what" "$where" done
of course, 1 might streamlined if concatenated strings or bar, used egrep. depends on you're looking for.
linux bash shell
No comments:
Post a Comment