Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用嵌入引号(源参数)运行命令时Bash脚本失败_Bash_Shell_Raspberry Pi_Sh_Avconv - Fatal编程技术网

使用嵌入引号(源参数)运行命令时Bash脚本失败

使用嵌入引号(源参数)运行命令时Bash脚本失败,bash,shell,raspberry-pi,sh,avconv,Bash,Shell,Raspberry Pi,Sh,Avconv,我使用source将生成的变量插入到文件中的字符串中,以便从bash脚本中执行该字符串 我已经将生成的字符串与从命令行工作的字符串进行了比较,但我看不出有什么不同,但是BASH命令失败了,因为看起来参数在中间的某个地方混在一起了。 我在ice_名称字符串周围转义了双引号,因此它看起来与我回显它时使用的相同 我需要转义其他角色吗 在-ice\u name参数之前,它似乎被弄混了 这是命令 avconv -re -i test.mp3 -c:a libmp3lame -content_type au

我使用
source
将生成的变量插入到文件中的字符串中,以便从bash脚本中执行该字符串

我已经将生成的字符串与从命令行工作的字符串进行了比较,但我看不出有什么不同,但是BASH命令失败了,因为看起来参数在中间的某个地方混在一起了。 我在ice_名称字符串周围转义了双引号,因此它看起来与我回显它时使用的相同

我需要转义其他角色吗

-ice\u 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://:mypwd@icecast.servername.com/my/mount/point/url
不确定你是否需要文件beng来源,但以防万一,它在这里

#!/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
处理配置文件并生成流命令的脚本

#!/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:$icecast_password@$icecast_server:$icecast_port$icecast_mount_url"

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

# Run command
$test_cmd

在字符串中嵌入引号不会转义包装的字符;它们只是值中的文字字符。您需要为此使用阵列:

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:$icecast_password@$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"

在字符串中嵌入引号不会转义包装的字符;它们只是值中的文字字符。您需要为此使用阵列:

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:$icecast_password@$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"

在生成文件源后,用于生成命令的命令是什么?我怀疑你是文字引用的受害者<代码>回显“foo”和
bar='foo';echo$bar
不完全相同。只需使用$streamcmd-无echo。或者,您是否希望看到执行所有合并和连接的脚本将添加的脚本添加到原始问题在生成文件后,您使用什么命令生成命令?我怀疑你是文字引用的受害者<代码>回显“foo”和
bar='foo';echo$bar
不完全相同。只需使用$streamcmd-无echo。或者你想看一个脚本来完成所有的合并和连接将脚本添加到原始问题我会尝试一下-我在一个单独的对话中被告知,关于源命令,我可以直接执行一个变量来运行命令。这看起来完全不同。当它在屏幕上看起来很好时,我只是假设它是对的——错误表明它正在尝试处理命令,但在参数上失败你被告知错误。见高达姆!天才!。非常感谢——但愿我早点问你。昨晚一定花了2个小时反复讨论嵌入引号是主要问题——或者如果我没有带空格的参数,我会不得不使用这种方法吗?我想我应该重新命名我的问题标题,以便更准确地表达问题。在这种情况下,你可能会侥幸逃脱;如果扩展的命令没有特殊的全局字符,如
*
,只有用于分隔参数的空格,并且没有使用任何shell语法,如
&&
,则扩展
$test\u cmd
应生成有效的简单命令。不过,一般来说,变量应该只用于保存数据,而不用于保存可执行代码。对于其他一切,请使用数组和shell函数。我将尝试一下——在一次单独的对话中,有人告诉我,关于源命令,我可以通过直接执行一个变量来运行该命令。这看起来完全不同。当它在屏幕上看起来很好时,我只是假设它是对的——错误表明它正在尝试处理命令,但在参数上失败你被告知错误。见高达姆!天才!。非常感谢——但愿我早点问你。昨晚一定花了2个小时反复讨论嵌入引号是主要问题——或者如果我没有带空格的参数,我会不得不使用这种方法吗?我想我应该重新命名我的问题标题,以便更准确地表达问题。在这种情况下,你可能会侥幸逃脱;如果扩展的命令没有特殊的全局字符,如
*
,只有用于分隔参数的空格,并且没有使用任何shell语法,如
&&
,则扩展
$test\u cmd
应生成有效的简单命令。不过,一般来说,变量应该只用于保存数据,而不用于保存可执行代码。对于其他所有内容,请使用数组和shell函数。