[英]Missing characters using echo in shell script
I tried to write a shell script that runs commands using a specific like sudo su - user
so I used sudo -H -u user bash -c
and all is working well except some echo commands. 我试图编写一个shell脚本,该脚本使用特定的命令(例如
sudo su - user
运行命令,所以我使用了sudo -H -u user bash -c
,除了某些回显命令之外,其他命令都运行良好。
This is not working and I can't solve it 这不起作用,我无法解决
sudo -H -u mailq bash -c "cd /var/www/html; echo -e \"
#!/bin/bash
NAME=\"user\"
DIR=/var/www/html
cd \$DIR
\" > start"
it prints like 它打印像
#!/bin/bash
NAME=user
DIR=/var/www/html
cd
Compare your original implementation to the following: 将您的原始实现与以下内容进行比较:
sudo -H -u mailq sh -c 'cat >"$1"' _ /var/www/html/start <<'EOF'
#!/bin/bash
NAME=user
DIR=/var/www/html
cd "$DIR"
EOF
Because <<'EOF'
has the heredoc quoted, all contents within are literal. 因为
<<'EOF'
引用了heredoc,所以其中的所有内容都是文字。 There's no need to escape \\$DIR
to prevent it from being immediately evaluated. 无需转义
\\$DIR
以防止立即对其进行评估。
Not entirely clear what OP is asking, but there seems to be an issue with double and single quotes in the bash -c
arguments. 尚不清楚OP在询问什么,但是
bash -c
参数中似乎存在双引号和单引号的问题。
$ sudo -H -u mailq bash -c "cd /var/www/html; echo '
#!/bin/bash
NAME=\"user\"
DIR=\"/var/www/html\"
cd \$DIR
'>~/start"
Output will be written to ~/start
. 输出将被写入
~/start
。
$ cat ~/start
#!/bin/bash
NAME="user"
DIR="/var/www/html"
cd $DIR
Note, as @Charles_Duffy points out, this kinda script insertion/execution can be hazardous and difficult to debug. 请注意,正如@Charles_Duffy指出的那样,这种脚本插入/执行可能很危险并且很难调试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.