특수 문자 및 구문 분석 BUG - Bashscript

특수 문자 및 구문 분석 BUG - Bashscript
Failed to parse arguments: Argument to "--command/-e" is not a valid command: Text ended before matching quote was found for ". (The text was '"cpulimit')

이것이 터미널에서 다음 스크립트를 실행할 때 얻는 결과입니다.

    #!/bin/bash
read -p "Which program u want to limit its processes?" ProgrameName
read -p "Which limitation percentage u want for it ?" limitationPercentage  

getAllPIDRunUnderThisProgram=$( ps -e | grep "$ProgrameName" | awk '{print $1;}')
for i in $getAllPIDRunUnderThisProgram
   do
    gnomeTab+="  --tab -e \"cpulimit -p $i -l $limitationPercentage \" "  
   done

gnome-terminal $gnomeTab

그는 8번째 줄의 큰 따옴표 때문에 사용해야 하는 이스케이프 문자 "\"를 구문 분석할 수 없습니다. gnomeTab+=" --tab -e \"cpulimit -p $i -l $limitationPercentage \" "따라서 큰 따옴표는 반드시 나중에 사용해야 --tab -e " some commands "하고 구문 분석 문제가 발생하지 않도록 사용하는 솔루션이 있습니까? ?

답변1

첫 번째 줄을 다음으로 변경할 수 있습니다.

#!/bin/bash -xv

쉘이 인수를 해석하는 방법을 보여주도록 합니다.

이스케이프하는 대신(으로 이어짐 eval) 배열을 사용하여 옵션을 축적해야 합니다.

for i in $getAllPIDRunUnderThisProgram ; do
    gnomeTab+=(--tab -e "cpulimit -p $i -l $limitationPercentage")  
done

echo "${gnomeTab[@]}"

관련 정보