在 awk 腳本內的系統呼叫中傳遞括號和空格的路徑

在 awk 腳本內的系統呼叫中傳遞括號和空格的路徑

在我的簡單 awk 腳本中,我呼叫系統命令

#!/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);    }'

問題是 () 的錯誤

$./testhere.sh

/home/軟體/其他/新(小程式) /home/軟體/其他/新(小程式)

這是一個測試 /home/software/Other/new (Applet)

sh: -c: 第 0 行:意外標記附近出現語法錯誤(' sh: -c: line 0:ls /home/software/Other/new (Applet)'

當我修改該部分以便命令作為字串傳遞時

                               command= "ls new (Applet)"

                               system (command);

我遇到類似的錯誤:

$ ./testhere.sh

/home/software/Other/new(小程式)

/home/software/Other/new(小程式)

這是一個測試 /home/software/Other/new (Applet)

sh: -c: 第 0 行:意外標記附近出現語法錯誤(' sh: -c: line 0:ls new (Applet)'

我該如何解決這個問題?

答案1

您必須引用(使用“”或“”)對於在 awk 中實現調用的 shell 具有空格的單字system():例如:

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

或者

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

相關內容