简体   繁体   中英

Find shell script and rename row

I have shell script with name script.sh. I need to find this script and change value inside script

From:

bash $current_dir/run.sh

to

./run.sh.x

my solution is something like:

find -maxdepth 10 -name "script.sh" -exec sed -i 's#bash '$current_dir'#run.sh#.#runs.sh.x#' {} \ ;

But still my command does not work. Any idea how to replace this string in new string in my shell script?

Your sed expression seems to be the issue.

You can use:

find . -maxdepth 10 -name "script.sh" -exec sed -i 's#bash \$current_dir/run\.sh#./run.sh.x#' {} +

@anubhava is correct. Another sed command which you can use.

find . -maxdepth 10 -name "script.sh" -type f | xargs -I {} sed -i 's/.*\/run.sh/\.\/run.sh.x/g' {}

One way to sidestep sed compatibility issues is to rely on perl . Adopting an answer from BashFAQ #21 :

search='bash $current_dir/run.sh'
replace='./run.sh.x'
in="$search" out="$replace" find . -name script.sh -exec \
  perl -pi -e 's/\Q$ENV{"in"}/$ENV{"out"}/g' '{}' +

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