UNIXShell loop control & mdash; whilewhile loop. Syntax: [plain] whilecommanddoStatement (s) tobeexecutedifcommandistruedonecommand can be a judgment or a command, such as reading files. When co... UNIX Shell loop control-while loop, syntax: [plain] while command do Statement (s) to be executed if command is true done command can be a judgment, it can also be a command, such as reading files. If the command condition is true, execute the statement block in the loop. otherwise, exit the loop directly. Www.2cto.com instance 1. a simple while loop prints data from 0 to 10: [plain] pg while1.sh #! /Bin/ksh I = 0 while [$ I-le 10] do echo $ I = 'expr $ I + 1' done # EOF execute the script: [plain] sh while1.sh instance 2. I want to move all the files whose names contain if in the current directory to the iftest folder. In this example, a temporary file temp.txt is used to save the file queried from the current folder. after creation, delete the file. [Plain] pg mvfile. sh #! /Bin/ksh echo "moving all file which contain word if to directory iftest" # create temporary file temp.txt if [-f temp.txt]; then ""> temp.txt echo "clearing temp.txt file successful" else touch temp.txt echo "making file temp.txt successfull" fi # writing file temp.txt with file contain word if ls-l | grep '^-. * '| sed-n'/if/'p | awk' {print $9}'> temp.txt # moving file to directory iftest while read LINE do mv $ LINE. /iftest done <temp.txt # droping the temporary file after moving echo "moving file with if successfull" if rm-f temp.txt>/dev/null 2> & 1; then echo "removing file temp.txt successful" fi # EOF before executing this script, the file in the current directory is as follows: www.2cto.com [plain]... /shell> lf calculator. sh ifcp2.sh ifroot. sh param. sh child. sh * ifdirec. sh ifset. sh profile. sh data. file ifeditor. sh iftest/test/elif. sh ifelif. sh iftest2.sh testdirec/env_variable ifels. sh iftst. sh * test1/father. sh * ifinteractive. sh ifwr. sh tst/grepif. sh ifmkdir. sh if1.sh * welcome. sh grepstr. sh ifmkdir2.sh if2.sh 125017.sh ifcounter. sh ifparam. sh log.txt ifcp. sh ifpwd. after sh name.txt is executed, all files containing if are moved to the iftest Directory: www.2cto.com [plain]... /shell/iftest> lf elif. sh ifeditor. sh ifparam. sh ifwr. sh grepif. sh ifelif. sh ifpwd. sh if1.sh * ifcounter. sh ifels. sh ifroot. sh if2.sh ifcp. sh ifinteractive. sh ifset. sh ifcp2.sh ifmkdir. sh iftest2.sh ifdirec. sh ifmkdir2.sh iftst. sh * -- the end --
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.