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

checkout.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には明示的に設定しないと、デフォルトでbcpスタイルに設定されるスタイル(-m)フラグがあり、これらの追加文字が追加されます。

それでも疑問に思うのは、sudoを使用して実行すると1つのスタイル「bcp」がデフォルトに設定されているのですが、sudoなしで実行すると別のスタイル「水平」がデフォルトに設定される理由です。

おすすめ記事