如何将输入读取为数组并将其用作 shell 脚本中的变量?

How to read input as array and use it as variable in shell script?

我创建了一个 shell 脚本,用于使用 ImageMagick 从 PDF 创建缩略图。它将一步步指导您,然后根据您的输入创建缩略图。

现在我正在尝试重新制作“$page”部分,因为您当时只能使用一页(数字)作为输入。我希望脚本能够一次读取更多页面输入(用逗号或 space) 分隔的数字 - 将它们保存到数组中,然后将此数组用作 "CONVERT" 函数中的变量 ($page)...

有人有想法吗?

谢谢!

代码如下:

    # set function
CONVERT()
{
    num=0
    name=$(basename "$file" .pdf | cut -d- -f1 | sed 's/.$//' | tr ' ' '-')
    ext="png"
    mark='/path/to/the/watermark.png'
    if [[ ! -e "$path"/"$name"_$num.$ext ]]; then
        echo "Creating thumbnail ..."
        convert -quiet -density 300 -quality 100 -thumbnail x1815 -background white -alpha remove "$file"[$page] "$path"/"$name"_$num.$ext
        else
            while [[ -e "$path"/"$name"_$num.$ext ]]; do
                (( num++ ))
            done
            echo "Creating thumbnail ..."
            convert -quiet -density 300 -quality 100 -thumbnail x1815 -background white -alpha remove "$file"[$page] "$path"/"$name"_$num.$ext
    fi

    while :
    do
        read -n 1 -s -p "Add watermark? [Y/n]" watermark
        case $watermark in
            ""|[yY] )
                echo ""
                echo "Adding watermark to thumbnail ..."
                convert -quiet "$path"/"$name"_$num.$ext "$mark" -gravity center -composite "$path"/"$name"_$num.$ext
                echo "DONE!"
                exit;
                ;;
            [nN] )
                echo ""
                echo "DONE!"
                exit;
                ;;
            * )
                echo ""
                echo "Use only 'y' or 'n' !"
        esac
    done
}

# start
echo ""
while :
do
    read -e -p "Path to PDF: " file
    if [[ -d "$file" ]]; then
        echo "'$file' is a directory"
        echo "Enter path to PDF File!"
    elif [[ -z "$file" ]]; then
            echo "Enter path to file!"
    elif [[ "$file" = /* ]]; then
        file=$(echo "$file" | tr -d "\")
            break
        else
            echo "Enter FULL path!"
    fi
done

while :
do
    read -e -p "Page to be converted (leave blank for first): " page
    if [[ -n ${page//[0-9]/} ]]; then
        echo "Use numbers only!"
    elif [[ -z "$page" ]]; then
            page="0"
            break
        else
            break
    fi
done

while :
do
    read -e -p "Path to save: " path
    if [[ -f "$path" ]]; then
        echo "'$path' is file"
        echo "Enter path to save thumbnail into!"
    elif [[ -z "$path" ]]; then
            path='/path/to/your/folder'
            break
        else
            break
    fi
done

CONVERT

read 有一个 -a 选项来读入数组:

$ read -r -p 'Enter page numbers: ' -a pages
Enter page numbers: 1 2 3 4 5
$ declare -p pages
declare -a pages=([0]="1" [1]="2" [2]="3" [3]="4" [4]="5")

在 Imagemagick 中,如果您愿意使用与输入名称相同的输出名称并附加数字索引,则可以在一个命令行中为多张图像或一张图像的页面添加水印和缩略图。为此,您在输入后包含一个 null: 分隔符,并使用 -layers composite 而不仅仅是 -composite 来添加水印。

输入:

在这里,我使用特殊的 Imagemagick internal rose: image 为动画中的第二张和第三张图像(如果需要,也可以是 PDF 文件)添加水印,然后将其缩略图缩小 50%。您可以用任何适当的 image.suffix.

替换 rose:
convert 3images.pdf[1,2] null: rose: -gravity center -compose over -layers composite -thumbnail 50% 3images_thumbs_%d.png


结果 2 图片:

3images_thumbs_1.png

3images_thumbs_2.png

我终于知道怎么做了!

    # set functions
CONVERT()
{
    num=0
    name=$(basename "$file" .pdf | cut -d- -f1 | sed 's/.$//' | tr ' ' '-')
    ext="png"
    mark='/path/to/the/watermark.png'
    for page in ${pages[*]}
        do
            if [[ ! -e "$path"/"$name"_$num.$ext ]]; then
                echo "Creating thumbnail ..."
                convert -quiet -density 300 -quality 100 -thumbnail x1815 -background white -alpha remove "$file"[$page] "$path"/"$name"_$num.$ext
                while :
                do
                    read -n 1 -s -p "Add watermark? [Y/n]" watermark
                    case $watermark in
                        ""|[yY] )
                            echo ""
                            echo "Adding watermark to thumbnail ..."
                            convert -quiet "$path"/"$name"_$num.$ext "$mark" -gravity center -composite "$path"/"$name"_$num.$ext
                            echo "DONE!"
                            break
                            ;;
                        [nN] )
                            echo ""
                            echo "DONE!"
                            break
                            ;;
                        * )
                            echo ""
                            echo "Use only 'y' or 'n' !"
                    esac
                done
            else
                while [[ -e "$path"/"$name"_$num.$ext ]]; do
                    (( num++ ))
                done
                echo "Creating thumbnail ..."
                convert -quiet -density 300 -quality 100 -thumbnail x1815 -background white -alpha remove "$file"[$page] "$path"/"$name"_$num.$ext
                while :
                do
                    read -n 1 -s -p "Add watermark? [Y/n]" watermark
                    case $watermark in
                        ""|[yY] )
                            echo ""
                            echo "Adding watermark to thumbnail ..."
                            convert -quiet "$path"/"$name"_$num.$ext "$mark" -gravity center -composite "$path"/"$name"_$num.$ext
                            echo "DONE!"
                            break
                            ;;
                        [nN] )
                            echo ""
                            echo "DONE!"
                            break
                            ;;
                        * )
                            echo ""
                            echo "Use only 'y' or 'n' !"
                    esac
                done
            fi
        done
}

# start
echo ""
while :
do
    read -e -p "Path to PDF: " file
    if [[ -d "$file" ]]; then
        echo "'$file' is a directory"
        echo "Enter path to PDF File!"
    elif [[ -z "$file" ]]; then
            echo "Enter path to file!"
    elif [[ "$file" = /* ]]; then
        file=$(echo "$file" | tr -d "\")
            break
        else
            echo "Enter FULL path!"
    fi
done

while :
do
    read -e -p "Page to be converted (leave blank for first): " -a pages
    if [[ -z "$pages" ]]; then
            pages="0"
            break
        else
            break
    fi
done

while :
do
    read -e -p "Path to save: " path
    if [[ -f "$path" ]]; then
        echo "'$path' is file"
        echo "Enter path to save thumbnail into!"
    elif [[ -z "$path" ]]; then
            path='/path/of/your/choice'
            break
        else
            break
    fi
done

CONVERT

看起来有点"messy",但确实有效!如果您知道如何制作代码 "cleaner",请告诉我。