1

In Shell script I want to achieve something like below:

str="india,uk,us,uae"

I want to split it and concatenate each item as below and assign to some variable

newstr = '-myParam="india" -myParam="uk" -myParam="us" -myParam="uae"'

so that I can use above concatenated string in my next command as below

curl "admin/admin" "localhost" $newstr.

I found a way using local IFS and for loop but the variable updated inside loop is not retaining value outside of loop because it runs in a separate bash.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Ronnie
  • 302
  • 1
  • 4
  • 16

4 Answers4

3
str="india,uk,us,uae"
var=-myparam=\"${str//,/\" -myparam=\"}\"
echo $var
Michael Vehrs
  • 3,293
  • 11
  • 10
2

Read the params into an array:

IFS=, read -a params <<< "$str"

And then loop through them and store the command in an array:

for i in "${params[@]}"; do
   command+=(-myparam=\"$i\")
done

Now you can expand it using printf "${command[@]}":

$ printf "%s " "${command[@]}"
-myparam="india" -myparam="uk" -myparam="us" -myparam="uae"

That is, now you have to say:

curl "admin/admin" "localhost" "${command[@]}"

This is based on this answer by chepner: command line arguments fed from an array.

Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • 3
    It it weren't for the quotes, we could use `"${params[@]/#/-myparam=}"`. – Benjamin W. May 19 '16 at 06:30
  • It seems unlikely to me that OP really wants those quotes. But there aren't enough details to hazard a definite opinion. The array substitution suggestion has lots of merit. – rici May 19 '16 at 06:38
  • IFS=, read -a params <<< "$str" will reset the delimeter globally or only for this statement ? – Ronnie May 19 '16 at 06:47
  • 1
    @Ronnie just for this statement. You can test by saying `IFS=, read -a params <<< "a,b,c"` and then `read -a params <<< "a,b,c"`. After each one, execute `printf "%s\n" "${params[@]}"`. Prepending `IFS` to a command makes it affect just the scope of the command. – fedorqui May 19 '16 at 06:49
  • @BenjaminW. You were right. I did not want the quotes and used the one line that you mentioned which solved my other issues with for loop. :) – Ronnie May 19 '16 at 10:29
  • Thanks @fedorqui for the quicky reply. – Ronnie May 19 '16 at 10:29
0

Below code would do :

$ str="india,uk,us,uae"
$ newstr=$(awk 'BEGIN{RS=","}{printf "-myParam=\"%s\" ",$1}' <<<"$str")
$ echo "$newstr"
-myParam="india" -myParam="uk" -myParam="us" -myParam="uae" 

Also when you pass new string as parameter to curl, double quote it to prevent word splitting and globbing, so do :

curl "admin/admin" "localhost" "$newstr"

Note: <<< or herestring is only supported in a few shells (Bash, ksh, or zsh) if I recall correctly. If your shell does not support it use echo,pipe combination.

sjsam
  • 21,411
  • 5
  • 55
  • 102
0
IFS=',' read -ra a <<< "${str//,/\",}";
curl "admin/admin" "localhost" "${a[@]/#/ -myParam=\"}\""

 

Explanation:

Starting with:

str="india,uk,us,uae";

Next, split the string into an array, using parameter substitution to insert " before each comma:

IFS=',' read -ra a <<< "${str//,/\",}";

Finally, we can get newstr through parameter substitution (while also appending the final "):

newstr="${a[@]/#/ -myParam=\"}\"";

newstr is now set to '-myParam="india" -myParam="uk" -myParam="us" -myParam="uae"'. We can skip the previous step and go straight to:

curl "admin/admin" "localhost" "${a[@]/#/ -myParam=\"}\""
Jeffrey Cash
  • 1,023
  • 6
  • 12