Howto pause bash loop execution and wait for any key (like ENTER for example) from user when running the script.
root@mongo2:~/tmp.loop# find -name '.bash*' | while read myfile; do echo "my variable is $myfile"; done my variable is ./.bashrc my variable is ./.bashrc_rado my variable is ./.bash_history my variable is ./.bash_tmp
Solution description and demonstration
We can use the standard 'read' bash built-in function (man bash). The problem is that it reads by default from the standard in (stdin, file descriptor 0).
We can use file redirection feature in bash to workaround this. Instead of reading from the stdin we can instruct the read to read from a different descriptor.
$echo -n 'ello' | ( read a; read -u1 b ; echo "1st read : - $a -"; echo "2th read : = $b =" ) test 1st read : - ello - 2th read : = test =
Unsuccessful version 1 showing the problem (read consumes our file names):
root@mongo2:~/tmp.loop# find -name '.bash*' | while read myfile; do echo "my variable is $myfile"; read ; done my variable is ./.bashrc my variable is ./.bash_history
Final solution (we press ENTER every time it pauses):
root@mongo2:~/tmp.loop# find -name '.bash*' | while read myfile; do echo "my variable is $myfile"; read -u1 ; done 23:48 my variable is ./.bashrc my variable is ./.bashrc_rado my variable is ./.bash_history my variable is ./.bash_tmpReferences
http://www.catonmat.net/blog/bash-one-liners-explained-part-three/
http://www.tldp.org/LDP/abs/html/index.html
http://www.catonmat.net/download/bash-redirections-cheat-sheet.pdf
No comments:
Post a Comment