分页: 1 / 2

参数传递问题求解

发表于 : 2012-03-03 15:00
自由建客
一个 bash 脚本,名为 notify-send2。
敲入以下命令测试

代码: 全选

notify-send2 -u critical "hello world"
结果总是不对。关键在 su 时的参数传递。
-------------------------------------------------------------------------------

代码: 全选

su "$user" -c "notify-send \"$*\"" 
su "$user" -c "notify-send $@"
这两种压根不对,提示
Missing argument for -u
这个应该是 notify-send 的输出,后面的 critical "hello world" 断掉了。

代码: 全选

>0< urd$ notify-send -u
Missing argument for -u

>1< urd$
-------------------------------------------------------------------------------

代码: 全选

su "$user" -c "notify-send '""$@""'"
更严重,提示
critical: -c: line 0: unexpected EOF while looking for matching `''
critical: -c: line 1: syntax error: unexpected end of file

-------------------------------------------------------------------------------

代码: 全选

su "$user" -c bash <<<"notify-send $@"
命令可行,但实际结果是 hello 和 world 分开了,导致 hello 成了标题 world 成了内容。

Re: 参数传递问题求解

发表于 : 2012-03-03 15:05
自由建客
附完整脚本

代码: 全选

#!/bin/bash

sendmsg()
{
	local IFS="=$IFS"
	while read line
	do
		read key value <<<"$line"
		value="${value#\'}"
		value="${value%\'}"
		case "$key" in
			unix-user)
				uid=$value
				;;
			active)
				active=$value
				;;
			x11-display)
				display=$value
				;;
			Session*:)
				[ "$active" != "TRUE" ] && continue
				export DISPLAY="$display"
				user=`sed -n 's/^\([^:]\+\):[^:]\+:'"$uid"':.*/\1/p' /etc/passwd`
				su "$user" -c "notify-send \"$*\""    # 问题就在这行
				;;
		esac
	done <<<"`ck-list-sessions | sed -n '/^Session\|\tunix-user =\|\tactive =\|\tx11-display =/p;$a\Session:\'`"
}

if [ "`id -u`" -eq 0 ]; then
	sendmsg "$@"
else
	exec notify-send "$@"
fi

Re: 参数传递问题求解

发表于 : 2012-03-03 15:06
tusooa
完了,id穿越了。 :em06

Re: 参数传递问题求解

发表于 : 2012-03-03 15:06
tusooa
建议你以后cp .com的链接的时候,去掉sid那一串。

Re: 参数传递问题求解

发表于 : 2012-03-03 15:11
tusooa
首先,建议不要用su.
sudo env DISPLAY=:0 bash -c 'notify-send "$@"' "$@"
行不

吾写的,删掉干啥。

Re: 参数传递问题求解

发表于 : 2012-03-03 15:15
自由建客
tusooa, 抱歉,我以为是那堆水鬼干的。
su 是必须的,这段脚本的目的就是能让守护进程给活动桌面发送消息。
http://forum.ubuntu.org.cn/viewtopic.php?f=21&t=365871
viewtopic.php?f=21&t=365871

Re: 参数传递问题求解

发表于 : 2012-03-03 15:34
tusooa
那就sudo -u "$user"这样的。

代码: 全选

sudo -u "$user" bash -c 'notify-send "$@"' "$@"
su的会很麻烦的。

Re: 参数传递问题求解

发表于 : 2012-03-03 15:44
自由建客
tusooa, 没看懂你那命令,但结果不对。
critical 成了标题,而不是紧急程度了。
sudo -u "$user" bash -c 'echo "$@"' "$@"
得到的只有
critical hello world
前面的 -u 没了

我现在暂时用临时文件搞定。

代码: 全选

				cmdfile=`mktemp`
				chmod 755 "$cmdfile"
				echo -n "notify-send " >"$cmdfile"
				for arg in "$@"
				do
					echo -n "'$arg' " >>"$cmdfile"
				done
				su "$user" -c "bash $cmdfile"
				rm -f "$cmdfile"

Re: 参数传递问题求解

发表于 : 2012-03-03 15:52
自由建客
TMD!我干吗要用临时文件!

代码: 全选

				cmd="notify-send "
				for arg in "$@"
				do
					cmd="$cmd '$arg'"
				done
				su "$user" -c bash <<<"$cmd"

Re: 参数传递问题求解

发表于 : 2012-03-03 16:02
tusooa
哦。那是参数给bash当opt了额

代码: 全选

sudo -u "$user" bash -c 'notify-send "$@"' -- "$@"

Re: 参数传递问题求解

发表于 : 2012-03-03 16:03
tusooa
自由建客 写了:TMD!我干吗要用临时文件!

代码: 全选

				cmd="notify-send "
				for arg in "$@"
				do
					cmd="$cmd '$arg'"
				done
				su "$user" -c bash <<<"$cmd"
你这样也不大好的。要是arg里有单引号,就完了。

Re: 参数传递问题求解

发表于 : 2012-03-03 16:14
自由建客
tusooa, 这下好了

代码: 全选

					cmd="$cmd \"`echo "$arg" | sed 's/"/\\\\"/g'`\""

Re: 参数传递问题求解

发表于 : 2012-03-03 16:24
自由建客
还是 sudo 好,还省得我要把 uid 转成 user

Re: 参数传递问题求解

发表于 : 2012-03-03 18:08
lilydjwg
要是我就用 Python 了。

Re: 参数传递问题求解

发表于 : 2012-03-04 0:21
tusooa
要是吾就用perl了。