使用 Sudo 运行 .sh 脚本会产生不同的输出

使用 Sudo 运行 .sh 脚本会产生不同的输出

我在我正在处理的脚本中看到一些非常奇怪的行为。我拥有的是一个 svn 钩子,每当对 svn 存储库进行提交时就会运行该钩子。它运行 sqsh 命令来生成另一个脚本 (checkout.sh),然后执行该生成的脚本。如果我只是手动运行脚本,生成的脚本会得到如下结果:

/usr/bin/install -d /opt/www/targetRepo/svn/targetDir1; /usr/bin/svn 签出 http://mysite.com/svn/sourceRepo/targetDir1 /opt/www/targetRepo/svn/targetDir1;
/usr/bin/install -d /opt/www/targetRepo/svn/targetDir2; /usr/bin/svn 签出 http://mysite.com/svn/srcRepo/targetDir2 /opt/www/targetRepo/svn/targetDir2;

注意每行如何以分号结尾。

如果我运行相同的脚本但以/usr/bin/sudo -u myname

然后生成的脚本将每行末尾的分号替换为管道“|”特点。因此,当主脚本尝试实际执行生成的脚本时,它会失败并出现“无效的文件结尾”错误。

更新 以下是我尝试运行的脚本:

sudo-hook.sh

#!/bin/bash

/usr/bin/sqsh -o ./checkout.sh -w 999999 -h -S myserver -D mydb -U dbuser -P apassword -C "select distinct '/usr/bin/install -d /opt/www/targetRepo/svn/'+cast(p.id as varchar(32) )+'; /usr/bin/svn checkout http://mysite.com/svn/srcRepo/'+cast(p.id as varchar(32) )+' /opt/www/targetRepo/svn/'+cast(p.id as varchar(32) ) from frame_skus as sku join products as p on p.id=sku.frame_id join frame_colors as c on c.id=sku.color_id join frame_sizes as s on s.id=sku.size_id join value_options as cc on c.color_code_id=cc.id join product_files as img on img.product_id=p.id  and img.color_id=c.id and img.dtype='FRAME_IMAGE' join brands as b on p.brand_id=b.id and b.id=69;"
./checkout.sh

结帐.sh(生成的脚本):

/usr/bin/install -d /opt/www/targetRepo/svn/31903; /usr/bin/svn checkout http://mysite.com/svn/31903 /opt/www/targetRepo/svn/31903|
/usr/bin/install -d /opt/www/targetRepo/svn/31904; /usr/bin/svn checkout http://mysite.com/svn/31904 /opt/www/targetRepo/svn/31904|
/usr/bin/install -d /opt/www/targetRepo/svn/31905; /usr/bin/svn checkout http://mysite.com/svn/31905 /opt/www/targetRepo/svn/31905|

有人可以告诉我这里发生了什么吗?

答案1

好吧,我想我找到了问题。显然是 sqsh 添加了那些“|”人物。 sqsh 有一个样式 (-m) 标志,如果您没有明确设置它,那么它似乎默认为“bcp”样式,该样式会添加这些额外的字符。

我仍然想知道的是为什么它在使用 sudo 运行时默认为一种样式“bcp”,但在不使用 sudo 运行时默认为另一种样式“horizo​​ntal”。

相关内容