0
我正在尝试使用osascript脚本更改我的Mac OS X桌面背景。一切工作正常,如果我使用静态的一切。但是如果我尝试引入变量,一切都会变得不合时宜。如何在Bash中传递变量作为参数
我的代码如下。
#! /bin/bash
function random_wall()
{
path_name="/Users/Furkan/Pictures/Selection/"
my_number=$[RANDOM%$1+1]
my_wall="$path_name$2-$my_number.jpg"
sec1='tell application "System Events" to set picture of every desktop to ("'
sec1+=$my_wall
sec2='" as POSIX file as alias)'
sec1+=$sec2
echo $sec1
# This one does not work
osascript -e $sec1
# The one below works
osascript -e 'tell application "System Events" to set picture of every desktop to ("/Users/Furkan/Pictures/Selection/night-3.jpg" as POSIX file as alias)'
}
hour=$(date +"%H")
if [ $hour -ge "5" ] && [ $hour -le "9" ]
then
random_wall 8 early-morning
fi
if [ $hour -ge "9" ] && [ $hour -lt "13" ]
then
random_wall 17 morning
fi
if [ $hour -ge "13" ] && [ $hour -lt "17" ]
then
random_wall 19 midday
fi
if [ $hour -ge "17" ] && [ $hour -lt "20" ]
then
random_wall 15 afternoon
fi
if [ $hour -ge "20" ] || [ $hour -lt "5" ]
then
random_wall 19 night
fi
我检查了输出。下面的代码来测试它,静态文件版本按预期工作。但是,当我尝试将我的变量作为参数传递时,我收到以下错误。
4:4: syntax error: Expected expression but found end of script. (-2741)
总之,我很困惑如何处理单引号和双引号的参与。
顺便说一句,http://shellcheck.net/会告诉你这样做。 –
@CharlesDuffy哇,谢谢。这非常有帮助! – Furkanicus