我想用bash编写一个启动脚本,以使ubuntu盒子对爷爷更友好。
脚本应:
打开镀铬等到Chrome关闭关闭计算机
到目前为止,我有
#!/bin/bash
if [ -z "$1" ]; then
address="http:\www.google.co.uk"
else
address=$1
fi
echo starting the internet
google-chrome $address
while [ 1=1 ];
do
grep_resp=$(ps aux | grep chrome)
if [ -z "$grep_resp" ]; then
echo turning computer off
else
echo chrome still running
fi
sleep 5
done
但是"铬"的 grep 在进程列表中
有什么援助吗?
您收到该错误是因为$grep_resp
if [ -z $grep_resp ]; then
。可能扩展为包含空格的字符串。 如果你在它周围加上引号:
if [ -z "$grep_resp" ]; then
它会做你所期望的。 但是,我认为所有这些都可能是不必要的。 Chrome 在运行时不会自动设置背景或任何内容,因此您应该能够执行以下操作:
google-chrome $address
echo turning computer off
...
怎么样:
#!/bin/bash
user=gramps
sudo -u "$user" -- chrome "$1"; shutdown -P now
脚本以根用户身份运行的位置。
第
14 行应该是
if [ -z "$grep_resp" ]; then
它需要用引号引起来,因为字符串中有空格。