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