2

I have a file (commands) containing a list of commands to be executed with one command per line. I need to parse each line with my bash script (main.sh) to maintain the parameters intact, so that I can pass them to another bash script (helper.sh).

For example:

If commands contains:

echo -e "hello world\n"
cat path_to/some\ file
...

I need main.sh to be able to distinguish the parameters in each line as a bash script normally would (desired results):

for line 1:
command: echo
$1: -e
$2: hello world


for line 2:
command: cat
$1: path_to/some file

So that I can store them in an array and pass them to helper.sh properly.

So far, I've ended up with something like (current results):

for line 1:
command: echo
$1: -e
$2: "hello
$3: world
"

for line 2:
command: cat
$1: path_to/some\
$2: file
rom58
  • 103
  • 2
  • 6
  • Can you provide your `commands` file as an example? – itChi Jan 16 '19 at 15:41
  • 1
    The problem is that once you "stringify" a command, it's really hard to unpack it (as bash would see it) without actually executing the command (with eval). You want to perform the [Shell Expansions](https://www.gnu.org/software/bash/manual/bashref.html#Shell-Expansions) steps. I'm not aware of a tool or scripts that can do that. – glenn jackman Jan 16 '19 at 15:50
  • Ah, I did figure out a way, but Charles closed this question. Shrug. The basis was to read the file with `while IFS= read -r line` and then extract the contents into an array with `declare -a words=($line)` -- variable is unquoted. – glenn jackman Jan 16 '19 at 16:00

1 Answers1

1

Python has a convenient module, shlex, for this. Try something like:

$ cat shparse.py
import shlex
import fileinput

for line in fileinput.input():
    parts = shlex.split(line)
    print(f'command: {parts[0]}')
    argument_number = 1
    for argument in parts[1:]:
        print(f'${argument_number}: {argument}')
        argument_number += 1
$ cat commands.txt
echo -e "hello world\n"
cat path_to/some\ file
$ python3 shparse.py <commands.txt
command: echo
$1: -e
$2: hello world\n
command: cat
$1: path_to/some file
Jon
  • 3,573
  • 2
  • 17
  • 24