0

I am a novice in shell scripting. Need help!!

I have a html file with multiple urls like https://test.abc.net/xxx/999994236/styles/css/the-guide-styles-responsive.min.css

I want to create a shell script in which I can pass a new version number which will replace the existing version number (shown in BOLD) in all the such urls.

This is what I have done till now taking just one url as of now;

#!/bin/bash

version=$1 ##Taking version as a parameter
my_str="https://test.abc.net/xxx/**999994236**/styles/css/the-guide-styles-responsive.min.css"
IFS='/' #setting slash as delimiter
read -a strarr <<<"$my_str" #reading str as an array as tokens separated by IFS
echo "Version : ${strarr[4]} "
strarr[4]=$1
echo "Version : ${strarr[4]} "
SAVE_IFS="$IFS"
IFS="/"
my_str_join="${my_str[*]}"
IFS="$SAVE_IFS"
echo "$my_str_join"

my_str_new="https://test.abc.net/xxx/**${strarr[4]}**/styles/css/the-guide-styles-responsive.min.css"
SAVE_IFS="$IFS"
IFS="/"
my_str_new_join="${my_str_new[*]}"
IFS="$SAVE_IFS"
echo "$my_str_new_join"

sed -i 's~${my_str_join}~${my_str_new_join}~g' index1.html ##This is where I am stuck. 

If I put actual urls in place of variables ${my_str_join} & ${my_str_new_join}, this step works fine but with variables it doesn't.

I have tried everything and can't brainstorm anymore. How should I go ahead with this? Please help!

0

The sed-command

sed -i 's~${my_str_join}~${my_str_new_join}~g' index1.html

uses single-quotes, which prevent substitution. Use double-quotes where you intend substitution, e.g.,

sed -i 's~${my_str_join}~'"${my_str_new_join}"'~g' index1.html
4
  • It worked!. Thanks!
    – Saurabh
    Apr 18 '20 at 10:15
  • If the answer was useful, you could mark it accepted. Apr 18 '20 at 10:18
  • Done! Can someone tell if the approach I am following is correct or a better short way is possible? I basically need to do the above with all the urls available in the html file. I am trying to fetch all the urls available in html file and save them in an array and then loop thru that array.
    – Saurabh
    Apr 18 '20 at 12:55
  • I'd use some tool (such as "lynx -dump") to get the urls, and construct a sed script using those (e.g., a text-file) and use sed. A loop would be far slower. Apr 18 '20 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.