I have a bash script (test.sh) which reads parameters from the command line.
The parameters could look like this: param1 param2 param3&ext param4 param5

The problem is that when the script reads the parameters and then echo them I get the following:

param1
param2
param3

No command 'ext' found did you mean:
<different commands from different packages>
ext: command not found
[1]+ Done                   ./test.sh param1 param2 param3

I want the output to look like this:

param1
param2
param3&ext
param4
param5

The source code of my script is:

#!/bin/bash
#
# test script
#
echo $1
echo $2
echo $3
echo $4
echo $5
#
# end of test script
#

hi,

& has to be protected (it's a metacharacter), either using (single )quotes, or slash:

echo 'param&ext'
echo "param&ext"
echo param\&ext

This was a simplified example.
The real problem consist of the following input:
[12:31:05.281] GET http://nordond11b-f.akamaihd.net/z/no/open/6b/6baae3762c2bd9485225cd545b78f86aa49b7d86/6baae3762c2bd9485225cd545b78f86aa49b7d86_,141,316,563,1266,2250,.mp4.csmil/manifest.f4ore=2.7.6&g=EHSZWFUPDAFAm?hdc [HTTP/1.0 200 OK 171 ms]

Here I want to divide this input as given below:

Parameter 1: [12:31:05.281]
Parameter 2: GET
Parameter 3: http://nordond11b-f.akamaihd.net/z/no/open/6b/6baae3762c2bd9485225cd545b78f86aa49b7d86/6baae3762c2bd9485225cd545b78f86aa49b7d86_,141,316,563,1266,2250,.mp4.csmil/manifest.f4m?hdcore=2.7.6&g=EHSZWFUPDAFA 
Parameter 4: [HTTP/1.0 
Parameter 5: 200 
Parameter 6: OK 
Parameter 7: 171 
Parameter 8: ms]

But when I try to run my script I get the following output:

[1] 6031
bash: [HTTP/1.0: No such file or directory
root@WS-LINUX:~# parameter 1 [12:31:05.281]
parameter 2 GET
parameter 3 http://nordond11b-f.akamaihd.net/z/no/open/6b/6baae3762c2bd9485225cd545b78f86aa49b7d86/6baae3762c2bd9485225cd545b78f86aa49b7d86_,141,316,563,1266,2250,.mp4.csmil/manifest.f4m?hdcore=2.7.6
parameter 4
parameter 5
parameter 6
parameter 7
parameter 8

The source code of the script is:

#!/bin/bash

echo parameter 1: $1
echo parameter 2: $2
echo parameter 3: $3
echo parameter 4: $4
echo parameter 5: $5
echo parameter 6: $6
echo parameter 7: $7
echo parameter 8: $8

How can I solve this "problem" ?
Is there some clever way of using the IFS environment variable perhaps ??

Edited 3 Years Ago by it@61@sec: Error in source

You can do this fairly easily with AWK. Consider the following AWK script:

{
    for (i = 1; i <= NF; i++) {
        printf("Parameter: %d: %s\n", i, $i)
    }
}

And run that like: awk -f t.awk t.in where teh above is in t.awk and your input is in t.in you get:

Parameter: 1: [12:31:05.281]
Parameter: 2: GET
Parameter: 3: http://nordond11b-f.akamaihd.net/z/no/open/6b/6baae3762c2bd9485225cd545b78f86aa49b7d86/6baae3762c2bd9485225cd545b78f86aa49b7d86_,141,316,563,1266,2250,.mp4.csmil/manifest.f4ore=2.7.6&g=EHSZWFUPDAFAm?hdc
Parameter: 4: [HTTP/1.0
Parameter: 5: 200
Parameter: 6: OK
Parameter: 7: 171
Parameter: 8: ms]

As you want.

L7Sqr: That's exactly what I was looking for. Thanx!
Watael: To use quoting around the script's argument is not an option in this case.. :-(

L7Sqr: What if the parameter should be given from standard input device and not from a file) ?
I should also be able to process parameter 3 further with another script.

Edited 3 Years Ago by it@61@sec: Request for new information

then the shell will keep on interpreting special characters as metacharacters and control operators.

If you allow the shell to process the string before it is passed to another facility you will always be subject to the rules of shell argument parsing. You can use the suggestions above to avoid that (quoting) or redirect to a file (or pipe) and read from that instead. For example:

$ ./foo.sh 
Some thing with an ampersand&some other text

$ ./foo.sh | awk -f t.awk 
Parameter: 1: Some
Parameter: 2: thing
Parameter: 3: with
Parameter: 4: an
Parameter: 5: ampersand&some
Parameter: 6: other
Parameter: 7: text

The nice thing about awk is that you can call shell facilities from within the script. So if you want to pass $3 to another script you can do it from within the awk script itself :)

Finally I decided to use the answer given by Watael which seems to suit my needs best.
I wanted to use parameter 3 as input to my script which calles AdobeHDS.php with some other fixed parameters as well. Here is the source code of my script:

#!/bin/bash

read -a ar <<< "$@"

php "/home/username/Documents/PHP Scripts/AdobeHDS.php" --quality high --delete --manifest ${ar[2]}

Suggestions for improvements are welcome!!!

This question has already been answered. Start a new discussion instead.