linux - Optional Command line parameter Shell -
lets call following replace.sh
if "$1" !="" replace_as=$1 else replace_as="tebow" fi find . ! -regex ".*[/]\.svn[/]?.*" -type f -print0 | xargs -0 -n 1 sed -i -e 's/sanchez/'$replace_as'/g' sorry primitive question. trying make command line parameter optional . ie if doesn't put , runs script uses tebow. seems work. if run script command line argument doesnt work.
ie
./test.sh
this replace tebow.
however ./test.sh smith
will not replace sanchez string smith
you can use shell parameter expansion , notation:
replace_as="${1:-tebow}" to in 1 line in 6.
additionally, code written should be:
if [ "$1" != "" ] replace_as="$1" else replace_as="tebow" fi the test command [; needs spaces around operands , ] @ end. need quotes around "$1" in case contains spaces; quotes around "tebow" optional because doesn't contain spaces (but uniformity good).
there's nothing stop writing:
xargs -0 -n 1 sed -i -e 's/sanchez/'"${1:-tebow}"'/g' but clarity of variable good, if you'll refer several times.
also, leave pipe @ end of line , start second command (xargs) on second line clarity (again - important).
find . ! -regex ".*[/]\.svn[/]?.*" -type f -print0 | xargs -0 -n 1 sed -i -e 's/sanchez/'"$replace_as"'/g' sometimes, not often, i'll indent second command. note double quotes around "$replace_as"; prevents problems spaces in replacement text.
Comments
Post a Comment