2016-06-21 1 views
0

사용자, zsh 및 zprezto를 설치하고 구성하는 스크립트를 작성하고 있습니다. 내 필요에 따라 사용자 X (su 포함) 및 zsh 쉘로 여러 명령을 실행해야하는 코드를 작성했습니다.이상한 bash 동작 (pipe, su, zsh) - 댓글과 사용자의 오류가 변경되지 않았습니다.

동작이 예상 한 것과 다르므로이 코드에 대한 설명이 필요합니다.

su admin -c zsh << EOF 
echo '$USER'; 
echo '$SHELL'; 
EOF 

이 코드는 테스트 목적으로 작성되었습니다. 사용자 관리와 zsh 쉘에서 명령을 실행해야합니다. 그러나 출력은 다음과 같습니다.

root 
/bin/zsh 

나는 이해하지 못합니다 : 명령을 실행하기 전에 사용자를 변경해서는 안됩니까?

심지어이 문제를 내 코드를 작성하는 시도와 내가 다른 이상한 행동 가지고 : 출력은 이상한 너무 그러나

cat << EOF | su "admin" -c zsh 

local file="${ZDOTDIR:-$HOME}/.zpreztorc" 
echo "File for $USER : ${ZDOTDIR:-$HOME}/.zpreztorc" 

setopt clobber; # Do not warn when overwritting file 
modules=$(cat "$file" | grep -Pzo "(?s)(zstyle ':prezto:load' pmodule\N*.)([\s\t]*'[a-z]*'[\s\t]*\\\\.)*[\s\t]*'[a-z]*'"); 
firstLineNumber=$(cat "$file" | grep -Fn "$(echo -n "$modules" | head -n 1)" | sed 's/^\([0-9]\+\):.*$/\1/'); 
lastLineNumber=$(cat "$file" | grep -Fn $(echo -n "$modules" | tail -n 1) | sed 's/^\([0-9]\+\):.*$/\1/'); 

for module in ${prezto_modules[@]}; do 
    modules="$modules \\ 
    '$module'"; 
done 

fileContent=$(cat "$file" | sed "$firstLineNumber,$lastLineNumber d"); 

echo -n "$fileContent" | head -n "$((firstLineNumber-1))" > "$file"; 
echo "$modules" >> "$file"; 
echo -n "$fileContent" | tail -n "+$((firstLineNumber))" >> $file; 

cat "$file"; 
EOF 

입니다 :

cat: '': No such file or directory 
cat: '': No such file or directory 
cat: '': No such file or directory 
grep: loaded: No such file or directory 
grep: loaded: No such file or directory 
grep: loaded: No such file or directory 
grep: loaded: No such file or directory 
grep: loaded: No such file or directory 
grep: loaded: No such file or directory 
grep: loaded: No such file or directory 
grep: autoloaded: No such file or directory 
grep: autoloaded: No such file or directory 
grep: autoloaded: No such file or directory 
grep: autoloaded: No such file or directory 
grep: autoloaded: No such file or directory 
grep: autoloaded: No such file or directory 
cat: '': No such file or directory 
sed: -e expression n°1, caractère 1: commande inconnue: `,' 
tail: incorrect line number: « + » 
File for root : /root/.zpreztorc 

나는 프랑스어, 돈의 오류를 번역하는 노력 나는 sed에 대한 정확한 traduction을 알지 못하므로 그냥 그대로 두었다. 그러나 오류 자체입니다하지 이상하다, 내 첫 번째 에코 라인을보고 무엇을 :

에코 "$ 사용자를위한 파일 : $. {ZDOTDIR : - $ 홈}/zpreztorc"-> "루트 파일 :/루트/.zpreztorc " 우리가 루트라는 사실을 제외하면 출력의 마지막 줄로 표시됩니다. 코드를 실행하기 전에 오류가 발견되었음을 의미합니다. 더 이상

뭔가 : 우리는 코드를 언급하는 경우, 오류가 계속 발견됩니다

su "admin" -c zsh << EOF 
# modules=$(cat "$file" | grep -Pzo "(?s)(zstyle ':prezto:load' pmodule\N*.)([\s\t]*'[a-z]*'[\s\t]*\\\\.)*[\s\t]*'[a-z]*'"); 
EOF 

출력은 다음과 같습니다

cat: '': No such file or directory 

어떻게 그렇게 설명 할 수 있을까? 감사

다음

답변

2

있는 문서 << 'MARKER'가 하나의 인용 문자열로 해석됩니다 동안 << MARKER은 큰 따옴표 문자열로 해석됩니다

su admin -c zsh << 'EOF' 
    echo "$USER" "this is admin" 
EOF 

<< MARKER를 사용하여 표준 입력으로 보내기 전에 확장되는 동안

su admin -c zsh << EOF 
    echo "$USER" "this is the current user single quotes doesn't prevent it" 
    echo '$USER' 'This is still expanded before send as stdin to zsh' 
EOF 

자세한 내용은 man bash | grep --max-count=1 '<<' -A 11을 참조하십시오.

  <<[-]word 
        here-document 
      delimiter 

    No parameter and variable expansion, command substitution, arithmetic 
    expansion, or pathname expansion is performed on word. If any charac‐ 
    ters in word are quoted, the delimiter is the result of quote removal 
    on word, and the lines in the here-document are not expanded. If word 
    is unquoted, all lines of the here-document are subjected to parameter 
    expansion, command substitution, and arithmetic expansion, the charac‐ 
    ter sequence \<newline> is ignored, and \ must be used to quote the 
    characters \, $, and `. 
+0

고마워, 그게 내가 필요한거야! 그러나, 이유는 무엇입니까 : su admin -c zsh << 'EOF' echo "$ SHELL"; EOF 은 출력이 : /bin/bash – Wargtek

+0

@Wargtek'su'는'/ bin/bash' 사용자 쉘을 시작하고'-c'는'zsh' 명령을 실행하기 때문에 다음과 같이 입력하면됩니다 : 쉘로'/ bin/bash'를 가지고있는 사용자와'zsh -c 'echo $ SHELL'. – andlrc