Reputation: 11
Script :
#script.sh
clear
echo ----------------------------------------------
echo ***********"starting shell script"************
echo ----------------------------------------------
ls
curl --location --request POST "http://localhost:8081/adddetails" --header "Content-Type: application/json" --data "{ \"batch_id\": \"$1\", \"modality\": { \"modality\" : \"$2\" }, \"start_time\": \"$3\"} ] } "
echo \"$1\"
echo \"$2\"
echo \"$3\"
echo ----------------------------------------------
echo ***********"ending shell script"************
echo ----------------------------------------------
My input file from where I a taking parameter:
1 a 2015-12-31 18:30:00
here $3
should be 2015-12-31 18:30:00
but because of space it is breaking.
I tried many escape characters but nothing is working
I ran below command:
bash scripts.sh $(cat inputs.txt)
Upvotes: 0
Views: 44
Reputation: 212624
The script itself looks fine, the trouble is with the way you are calling it. bash
has no way of reading your mind to determine that you want the 3rd and 4th columns of your input to be merged into a single argument. Perhaps you could try separating the arguments by tab instead of spaces in inputs.txt and specify tab
in IFS. Something like:
$ cat -tve inputs.txt
1^Ia^I2015-12-31 18:30:00$
$ (IFS=$'\t'; ./script.sh $(cat inputs.txt) )
Another option (which doesn't require the subshell or modification of the input file) which seems a bit cleaner is:
$ read a b date < inputs.txt
$ ./script.sh "$a" "$b" "$date"
With this particular example, you don't need to set IFS because read
will concat all of the remaining arguments into the final variable, but you may want to use a similar technique as above and do:
$ IFS=$'\t' read a b date c < inputs.txt
$ ./script.sh "$a" "$b" "$date"
For example, this would be necessary if there were more inputs on the line after the date, and this would rely on modifying the input file to use tab
as the field separator.
Upvotes: 1
Reputation: 136
Shell's default behavior is to split args by space, so not much to do there, your $3
argument will be the date only.
What you could do is create a variable inside your script concatenating the variables you want to handle as a single value.
DATETIME="$3 $4"
echo $DATETIME
Another option would be to pass your arguments with quotes to the command:
./script.sh arg1 arg2 "YYYY-MM-DD hh:mm:ss"
But this approach doesn't work if you are passing the arguments from a file.
Upvotes: 1