2016-07-08 3 views
0

私はsourceを使用して、bashスクリプトからその文字列を実行するために生成された変数をファイルから文字列に挿入します。埋め込み引用符(ソースパラメータ)を含むコマンドを実行するとBashスクリプトが失敗する

私は生成された文字列をコマンドラインから動作するものと比較するためにエコーしましたが、違いは見えないようですが、bashコマンドは失敗しました。中間。

私はice_name文字列を二重引用符をエスケープしてきたので、それは私がそれ

をエコーするとき、私は他の文字をエスケープする必要がありますか働くものと同じに見えますか?

これは、コマンド

avconv -re -i test.mp3 -c:a libmp3lame -content_type audio/mpeg -b:a 128k -legacy_icecast 1 
-ice_name "Raspi Test Stream of MP3" -f mp3 
icecast://:[email protected]/my/mount/point/url 

ない、それはあなたがソースのファイルベンが必要ですが、念のためにここにある場合は確実である、BEFORE -ice_nameパラメータ

を混入しているようです

#!/bin/bash 
# 
# stream.cfg 
# 
# WiFi Settings 
# 
wifi_name=mywifi 
wifi_password=mywifipwd 
# 
# Icecast Server Settings 
# 
icecast_server=icecast.server.com 
icecast_port=443 
icecast_mount_url=/user/mountpt/url 
icecast_show="RPi Demo Show - autostart" 
icecast_description="Test of Stream from RPi USB Audio to Spreaker" 
icecast_user="" 
# Source password 
icecast_password=sourcepwd 
# 
# avconv setting for Raspbian Jessie Lite 
# may not need if you're using a self compiled ffmpeg version 
# 
icecast_legacy=1 
# 
# Stream Settings - probably not safer to go higher unless great internet connection 
# 
stream_bitrate=128k 

設定ファイルを処理して、包まれた文字をエスケープしない文字列に引用符を埋め込むmコマンド

#!/bin/bash 
# 
# autostart-settings.sh 
# 
# Load in config file settings 

CONFIG_FILE=~/autostart/autostart-settings.cfg 

# Check if file exists 
echo "does file exist" 
if [ ! -f "$CONFIG_FILE" ]; then 
    echo "Config File: $(CONFIG_FILE) does not exist" 
    exit 1 
else 
    # process settings 
    echo "running source on $CONFIG_FILE" 
    source "$CONFIG_FILE" 
fi 

start_cmd="avconv -re -i /home/pi/test.mp3 -c:a libmp3lame -content_type audio/mpeg -b:a $stream_bitrate -legacy_icecast $icecast_legacy" 

stream_parameters="-ice_name \"$icecast_show\" -f mp3" 

icecast_setup="icecast://$icecast_user:[email protected]$icecast_server:$icecast_port$icecast_mount_url" 

test_cmd="$start_cmd $stream_parameters $icecast_setup" 
echo "Testing command: $test_cmd" 

# Run command 
$test_cmd 
+0

ファイルをソースした後にコマンドを生成するために使用するコマンドは何ですか?私はあなたが文字通りの引用に犠牲になっていると思う。 'echo" foo "'と 'bar = '" foo "'; echo $ bar'は同一ではありません。 – chepner

+0

ちょうど$ streamcmdを使用しています - エコーはありません。または、すべてのマージと連結を行うスクリプトを参照したいですか? – dbmitch

+0

オリジナルの質問に追加されたスクリプト – dbmitch

答えて

1

。それらは値の単なるリテラル文字です。

cmd=avconv 
args=(-re -i /home/pi/test.mp3 -c:a libmp3lame -content_type audio/mpeg -b:a "$stream_bitrate" -legacy_icecast "$icecast_legacy") 

stream_parameters=(-ice_name "$icecast_show" -f mp3) 

icecast_setup="icecast://$icecast_user:[email protected]$icecast_server:$icecast_port$icecast_mount_url" 

test_cmd="$start_cmd $stream_parameters $icecast_setup" 
echo "Testing command: $cmd ${args} ${stream_parameters[@]} $icecast_setup" 

# Run command 
"$cmd" "${args[@]}" "${stream_parameters[@]}" "$icecast_setup" 
+0

私はこれを試してみます - 私は別の会話で、ソースコマンドについて質問しました。直接1つの変数。 **これは根本的に違います**。私はちょうど画面上で見た目が良かったと思っていました - そして、エラーはコマンドを処理しようとしていたが、パラメータに失敗していることを示しました – dbmitch

+0

あなたは間違っていました。 [私は変数にコマンドを入れようとしていますが、複雑なケースは常に失敗します!](http://mywiki.wooledge.org/BashFAQ/050) – chepner

+0

Gawdam!天才!。どうもありがとうございます。昨夜2時間以上何度も何度も過ごしていたに違いありません – dbmitch

関連する問題