![명령을 n번 반복한 후 종료하는 방법은 무엇입니까?](https://linux55.com/image/94287/%EB%AA%85%EB%A0%B9%EC%9D%84%20n%EB%B2%88%20%EB%B0%98%EB%B3%B5%ED%95%9C%20%ED%9B%84%20%EC%A2%85%EB%A3%8C%ED%95%98%EB%8A%94%20%EB%B0%A9%EB%B2%95%EC%9D%80%20%EB%AC%B4%EC%97%87%EC%9E%85%EB%8B%88%EA%B9%8C%3F.png)
설치를 자동화하고 싶고 gksu를 사용하여 다운로드한 설치 프로그램을 실행해야 합니다. 나는 시도했다:
시도=0 ~까지 gksu 명령; 시도=$((시도+1)) if["$try"-gt 3]; 1번출구 필리핀 제도 완벽한
하지만 세 번째 시도까지 종료되지 않습니다. gksu가 종료 코드 0 또는 0이 아닌 종료 코드로 종료되는지 여부는 중요하지 않습니다. 내가 원하는 것은: 내가 이것을 어떻게 할 수 있는가?
while gksu command's exit code is not 0 and attempt number is not 3 repeat gksu command. If exit code is not 0 but attempt number is 3, exit the whole script. If exit code is 0 leave cycle and continue processing the script.
답변1
시간이 있으면 seq
다음과 같이 할 수 있습니다.
for attempt in $(seq 1 3)
do
gksu command && break
done
seq
사용할 수 없지만 bash가 있고 사용하려는 경우 :
for((attempt=1;attempt<=3;attempt++))
do
gksu command && break
done
더 간단하게 (드루벤):
for attempt in {1..3}
do
gksu command && break
done