简体   繁体   中英

Find and replace string in command line args in bash

I have bash script which takes set of command line argument. Now I want to remove some of the argument that I received from user and pass it to other script. How can I achieve this.

remove_arts.sh

echo "Prints all args passed $*"
# Add logic to remove args value --native/-nv/--cross
my_app_args=$(command to store result in $*)
bash run_my_app.sh my_app_args

So when I run that script with

bash remove_arts.sh --native -nv --execute_rythm

After processing bash run_my_app.sh --execute_rythm

I have checked Replace one substring for another string in shell script which suggested to use

first=${first//Suzy/$second}

But I am not able to use $second as empty string.

For each argument, store all arguments that are not in the list of excludes inside an array. Then pass that array to your script.

# Add logic to remove args value --native/-nv/--cross
args=()
for i in "$@"; do
  case "$i" in
  --native|-nv|--cross) ;;
  *) args+=("$i"); ;;
  esac
done
run_my_app.sh "${args[@]}"

You might want to research bash arrays and read https://mywiki.wooledge.org/BashFAQ/050 . Remember to check your scripts with shellcheck

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM