墙纸的外壳脚本,背景颜色平均



不久前,我编写了一个脚本,用于自动更改多显示器设置的壁纸。我想更改它,以便填充额外空间的颜色是图像的平均值,但是当我尝试时,它似乎杀死了脚本。这是我得到的:

#!/bin/sh
BACKGROUND="-background #452036"
GRAVITY="-gravity Center"
GRAVITY2="-gravity North"
GRAVITY3="-gravity South"
LEFT_SIZE=1920x1062
RIGHT_SIZE=1280x958
FINAL_SIZE=3200x1080
RANDOM=$$$(date +%s)
FILES=($1/*)
NUM_FILES=${#FILES[*]}
LEFT_IMAGE=${FILES[$RANDOM % $NUM_FILES]}
RIGHT_IMAGE=${FILES[$RANDOM % $NUM_FILES]}
LCOLOR=${convert $LEFT_IMAGE -resize 1x1! -depth 8 txt:- | tail -1 | grep -E -o "#(.)* " | cut -f1 -d" "}
RCOLOR=${convert $RIGHT_IMAGE -resize 1x1! -depth 8 txt:- | tail -1 | grep -E -o "#(.)* " | cut -f1 -d" "}
LBACKGROUND="-background" $LCOLOR
RBACKGROUND="-background" $RCOLOR

convert $LBACKGROUND $GRAVITY -scale $LEFT_SIZE ${LEFT_IMAGE}
    -extent $LEFT_SIZE ~/.left.png
convert $RBACKGROUND $GRAVITY -scale $RIGHT_SIZE ${RIGHT_IMAGE}
    -extent $RIGHT_SIZE ~/.right.png
convert $BACKGROUND $GRAVITY2 +append 
~/.left.png 
~/.right.png 
~/.wpcompo.png
convert $BACKGROUND $GRAVITY3 -extent $FINAL_SIZE ~/.wpcompo.png ~/.wallpaper.png

它返回:

/home/ryan/Scripts/wpconvert.sh: line 17: ${convert ${LEFT_IMAGE} -resize 1x1! -depth 8 txt:- | tail -1 | grep -E -o "#(.)* " | cut -f1 -d" "}: bad substitution

我假设您想在子外壳中运行转换,因此您需要$()而不是${}

LCOLOR=$(convert $LEFT_IMAGE -resize 1x1! -depth 8 txt:- | tail -1 | grep -E -o "#(.)* " | cut -f1 -d" ")
RCOLOR=$(convert $RIGHT_IMAGE -resize 1x1! -depth 8 txt:- | tail -1 | grep -E -o "#(.)* " | cut -f1 -d" ")

第 17 行和第 18 行:使用

LCOLOR=$(convert $LEFT_IMAGE -resize 1x1! -depth 8 txt:- | tail -1 | grep -E -o "#(.)* " | cut -f1 -d" ")
RCOLOR=$(convert $RIGHT_IMAGE -resize 1x1! -depth 8 txt:- | tail -1 | grep -E -o "#(.)* " | cut -f1 -d" ")

相反。你真的应该使用更多的引号!如果某些文件名包含空格,脚本将失败

最新更新