2016-06-21 10 views
0

ユーザ、zsh、zpreztoをインストールして設定するスクリプトを書いています。私のニーズに合わせて、複数のコマンドをユーザーX(suを使用)とzshシェルとして実行するコードを記述しました。奇妙なbashの動作(パイプ、su、zsh) - コメントとユーザのエラーは変更されていません

動作は私が期待したものではないため、このコードについての説明が必要です。

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

このコードは単なるテストのためだった:私は、コマンドは、ユーザーの管理者として、およびzshのシェルで実行されていることを確認する必要がありました。ただし、出力は次のとおりです。

root 
/bin/zsh 

私は理解していない:suがコマンドを実行する前に、ユーザーを変更するべきではないのですか?

私もこの問題に私のコードを書いてみましたし、私は別の奇妙な行動だ:しかし、出力があまりにも奇妙である

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のための正確なtraductionsを知っているので、私はそのままそれを聞かせてください。 エラーはそれ自体奇妙なものではありません。

echo "ファイル:$ USER:$ {ZDOTDIR: - $ HOME} /。zpreztorc" - > "root:/ root/.zpreztorc " 私たちがrootであることを除いて、出力の最後の行として表示されます。これは、コードを実行する前にエラーが見つかったことを意味します。

さらに奇妙な何か:私たちはコードにコメント場合は、エラーがまだ気づいています

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 

どのようにあなたがそれを説明するだろうか? << 'MARKER'は、単一引用符で囲まれた文字列として解釈される一方<< MARKERと おかげでここ

答えて

2

文書は、二重引用符を文字列として解釈されます:

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

関連する問題