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.

4 Answers 4

3
str="india,uk,us,uae"
var=-myparam=\"${str//,/\" -myparam=\"}\"
echo $var
Sign up to request clarification or add additional context in comments.

2 Comments

I believe this is the best and simplest way to go about it
I believe this is the simplest and shortest answer. But could you give a little explanation when you have time? Thanks
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.

6 Comments

It it weren't for the quotes, we could use "${params[@]/#/-myparam=}".
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.
IFS=, read -a params <<< "$str" will reset the delimeter globally or only for this statement ?
@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.
@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. :)
|
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.

Comments

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=\"}\""

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.