한 줄 Bash 무한 while 루프 구문
세미콜론 및 / 또는 중괄호의 올바른 조합을 찾는 데 문제가 있습니다. 이 작업을 수행하고 싶지만 명령 줄에서 한 줄로 표시합니다.
while [ 1 ]
do
foo
sleep 2
done
while true; do foo; sleep 2; done
그건 그렇고, 명령 프롬프트에서 여러 줄로 입력 한 다음 (표시되는 것처럼) 화살표를 위로 향한 히스토리를 호출하면 올바른 구두점으로 한 줄에 표시됩니다.
$ while true
> do
> echo "hello"
> sleep 2
> done
hello
hello
hello
^C
$ <arrow up> while true; do echo "hello"; sleep 2; done
while 상태에서 수면 명령을 사용할 수도 있습니다. 한 줄로 더 깔끔하게 보이게하는 임호.
while sleep 2; do echo thinking; done
콜론은 항상 "true"입니다.
while :; do foo; sleep 2; done
세미콜론을 사용하여 문을 구분할 수 있습니다.
$ while [ 1 ]; do foo; sleep 2; done
다음 until
명령 을 사용할 수도 있습니다 .
until ((0)); do foo; sleep 2; done
대조적으로 해당 주 while
, until
긴 테스트 조건이 0이 아닌 이탈 상태 인만큼 루프 내부의 명령어를 실행할 것이다.
while
루프 사용 :
while read i; do foo; sleep 2; done < /dev/urandom
for
루프 사용 :
for ((;;)); do foo; sleep 2; done
사용하는 또 다른 방법 until
:
until [ ]; do foo; sleep 2; done
아주 간단한 무한 루프 .. :)
while true ; do continue ; done
귀하의 질문은 다음과 같습니다.
while true; do foo ; sleep 2 ; done
나는 세미콜론을 WHILE 문에만 사용하고 && 연산자를 사용하여 루프가 두 가지 이상을 수행하도록 만들고 싶습니다.
그래서 항상 이렇게 해요
while true ; do echo Launching Spaceship into orbit && sleep 5s && /usr/bin/launch-mechanism && echo Launching in T-5 && sleep 1s && echo T-4 && sleep 1s && echo T-3 && sleep 1s && echo T-2 && sleep 1s && echo T-1 && sleep 1s && echo liftoff ; done
watch
대신 간단한 프로세스 관찰 사용
while 루프가 어떤 조건 이후에 중지되기를 원 foo
하고이 조건이 충족 될 때 명령이 0이 아닌 값을 반환하는 경우 다음과 같이 루프를 중단 할 수 있습니다.
while foo; do echo 'sleeping...'; sleep 5; done;
예를 들어 foo
명령이 일괄 적으로 항목을 삭제하고 삭제할 항목이 남아 있지 않으면 1을 반환합니다.
This works well if you have a custom script that needs to run a command many times until some condition. You write the script to exit with 1
when the condition is met and exit with 0
when it should be run again.
For example, say you have a python script batch_update.py
which updates 100 rows in a database and returns 0
if there are more to update and 1
if there are no more. The the following command will allow you to update rows 100 at a time with sleeping for 5 seconds between updates:
while batch_update.py; do echo 'sleeping...'; sleep 5; done;
Using while
:
while true; do echo 'while'; sleep 2s; done
Using for
Loop:
for ((;;)); do echo 'forloop'; sleep 2; done
Using Recursion
, (a little bit different than above, keyboard interrupt won't stop it)
list(){ echo 'recursion'; sleep 2; list; } && list;
If I can give two practical examples (with a bit of "emotion").
This writes the name of all files ended with ".jpg" in the folder "img":
for f in *; do if [ "${f#*.}" == 'jpg' ]; then echo $f; fi; done
This deletes them:
for f in *; do if [ "${f#*.}" == 'jpg' ]; then rm -r $f; fi; done
Just trying to contribute.
You can try this too WARNING: this you should not do but since the question is asking for infinite loop with no end...this is how you could do it.
while [[ 0 -ne 1 ]]; do echo "it's looping"; sleep 2; done
참고URL : https://stackoverflow.com/questions/1289026/syntax-for-a-single-line-bash-infinite-while-loop
'developer tip' 카테고리의 다른 글
IntelliJ는 마우스를 올리면 JavaDocs 툴팁을 표시합니다. (0) | 2020.10.03 |
---|---|
Git은 푸시 할 때마다 사용자 이름을 묻습니다. (0) | 2020.10.03 |
두 날짜 사이의 일 수 찾기 (0) | 2020.10.03 |
운영 체제 개발을 시작하기위한 리소스에는 어떤 것이 있습니까? (0) | 2020.10.03 |
C에서 size_t는 무엇입니까? (0) | 2020.10.03 |