
간단한 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); }'
문제는 ()의 오류입니다.
$./testthere.sh
/home/software/Other/new(애플릿) /home/software/Other/new(애플릿)
이것은 /home/software/Other/new(애플릿) 테스트입니다.
sh: -c: line 0: 예기치 않은 토큰 (' sh: -c: line 0:
ls /home/software/Other/new(Applet)' 근처에 구문 오류가 있습니다.
명령이 문자열로 전달되도록 부분을 수정했을 때
command= "ls new (Applet)"
system (command);
비슷한 오류가 발생합니다.
$ ./testthere.sh
/홈/소프트웨어/기타/신규(애플릿)
/홈/소프트웨어/기타/신규(애플릿)
이것은 /home/software/Other/new(애플릿) 테스트입니다.
sh: -c: line 0: 예기치 않은 토큰 (' sh: -c: line 0:
ls new(애플릿) 근처에 구문 오류가 있습니다.'
이 문제를 어떻게 해결하나요?
답변1
system()
awk에서 호출을 구현하는 쉘에 대해 공백이 있는 단어를 인용해야 합니다("" 또는 ''). 예:
system ("ls '" Test "'");
또는
system ("ls \"" Test "\"");