奇怪的bash行为(pipe、su、zsh)-注释错误和用户未更改

奇怪的bash行为(pipe、su、zsh)-注释错误和用户未更改,bash,pipe,zsh,eof,su,Bash,Pipe,Zsh,Eof,Su,我正在编写一个脚本来安装和配置用户zsh和zprezto。为了满足我的需要,我编写了一些代码,这些代码应该以userx(使用su)和zshshell的形式执行多个命令 这种行为不是我所期望的,所以我需要一些关于这段代码的解释 su admin -c zsh << EOF echo '$USER'; echo '$SHELL'; EOF 我只是不明白:在执行命令之前,su不应该更改用户吗 即使遇到这个问题,我也尝试编写代码,但我遇到了另一个奇怪的行为: cat << EO

我正在编写一个脚本来安装和配置用户zsh和zprezto。为了满足我的需要,我编写了一些代码,这些代码应该以userx(使用su)和zshshell的形式执行多个命令

这种行为不是我所期望的,所以我需要一些关于这段代码的解释

su admin -c zsh << EOF
echo '$USER';
echo '$SHELL';
EOF
我只是不明白:在执行命令之前,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
我试着翻译法语的错误,不知道sed的确切翻译,所以我就照原样翻译。 但错误本身并不奇怪,看看我的第一条回音线:

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
你怎么能解释呢?
谢谢这里有
的文档这里有
的文档谢谢,这正是我需要的!但是,为什么:su admin-c zsh@Wargtek因为
su
启动用户shell,即
/bin/bash
,而
-c
运行命令
zsh
,如果您键入:
zsh-c'echo$shell'
,并且用户具有
/bin/bash
作为shell。谢谢,这正是我需要的!但是,为什么:su admin-c zsh@Wargtek因为
su
启动用户shell,它是
/bin/bash
,而
-c
运行命令
zsh
,如果您键入:
zsh-c'echo$shell'
,并且用户具有
/bin/bash
作为shell,则会发生同样的情况。
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
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
su admin -c zsh << 'EOF'
  echo "$USER" "this is admin"
EOF
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
          <<[-]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 `.