Pass a path with parenthesis and space in system call inside awk script

0

Inside my simple awk script, I call system command

#!/bin/bash
 Test='/home/software/Other/new (Applet)'
 ls "${Test}"

 var=$(ls "${Test}")
 echo $var
 awk  -vTest="$var" 'BEGIN  {

              #some code that works

               print "This is a test", Test
               #command= "ls new (Applet)"
               system ("ls " Test);    }'

The problem is the error with the ()'s

$./testhere.sh

/home/software/Other/new (Applet) /home/software/Other/new (Applet)

This is a test /home/software/Other/new (Applet)

sh: -c: line 0: syntax error near unexpected token (' sh: -c: line 0:ls /home/software/Other/new (Applet)'

When I modified the part so that the command is passed as a string

                               command= "ls new (Applet)"

                               system (command);

I am getting similar errors:

$ ./testhere.sh

/home/software/Other/new (Applet)

/home/software/Other/new (Applet)

This is a test /home/software/Other/new (Applet)

sh: -c: line 0: syntax error near unexpected token (' sh: -c: line 0:ls new (Applet)'

How do I get around this?

newbie

Posted 2015-07-21T13:48:38.723

Reputation: 1

Answers

2

You must quote (with "" or '') the word that has spaces for the shell that is implementing the system() call in awk: eg:

system ("ls '" Test "'"); 

or

system ("ls \"" Test "\"");

meuh

Posted 2015-07-21T13:48:38.723

Reputation: 4 273