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
出力を確認しました。以下のコードは、静的なファイルのバージョンが意図したとおりに動作しているかどうかをテストします。しかし、変数として引数を渡そうとすると、次のエラーが発生します。
要約すると、私は一重引用符と二重引用符の関与を考慮して引数を処理する方法が混乱しています。
ちなみに、http://shellcheck.net/はこれを正確に行うように指示していました。 –
@CharlesDuffyうわー、ありがとう。これは非常に役に立ちます! – Furkanicus