디렉터리 및 조건의 파일을 사용하여 명령을 실행하는 Bash 스크립트

디렉터리 및 조건의 파일을 사용하여 명령을 실행하는 Bash 스크립트

테스트를 통해 Figlet 글꼴을 정렬하고 싶었기 때문에 Figlet 글꼴을 하나씩 살펴보고 마음에 들지 않는 글꼴을 제거하는 스크립트를 만들기로 결정했습니다. while 루프 내에서 올바른 if-then 조건에 대한 해결책을 찾으려고 하는데 찾을 수 없습니다. 다음은 스크립트 자체이지만 현재는 단일 스크롤에 있는 모든 글꼴의 예만 제공합니다.

#!/bin/bash
#script to test figlet fonts
rm /usr/share/figlet/list.txt #delete old list
ls /usr/share/figlet > /usr/share/figlet/list.txt #create new list
filename='/usr/share/figlet/list.txt'
n=1
while read line; do
    figlet -f $line Figlet
    echo -e "Press 0 if you don't like it, font will be deleted"
    read decision
    if [ "$decision" = "0" ]; then
        rm "/usr/share/figlet/$line"
        echo -e "Font deleted"
    else
        echo -e "Font saved"
    fi
    n=$((n+1))
done < $filename

답변1

초기 문제는 파일 목록의 내용이 입력되고 루프가 예상대로 작동하지 않는다는 것 read decision입니다 while. 그런데 왜 목록이 필요한가요?

파일을 반복하는 것이 좋습니다 for.

#!/bin/bash
for font in /usr/share/figlet/*; do
    figlet -f "$font" Figlet
    echo -e "Press 0 if you don't like it, font will be deleted"
    read decision
    if [ "$decision" = "0" ]; then
        rm "$font"
        echo -e "Font deleted"
    else
        echo -e "Font saved"
    fi
done

관련 정보