课程: Learning Linux Shell Scripting

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

The for loop

The for loop

- [Instructor] Let's take a look at the for loop. From the terminal type touch for.sh, change mode 755 for.sh, and atom for.sh. And we'll do our shebang. This time we'll take some parameters from the user. We could take the parameters individually using $1, $2 and so on but there is another special symbol, dollar sign at sign. So, here we're gonna say NAMES and we're gonna say equals $@. $@ holds all of the parameters the user typed in one array. We can then use this with the loop to iterate over each parameter one at a time. So, we say for NAME in $NAMES and then we say do and we're gonna have a done and then in between here we'll say echo hello $NAME, so this will print hello for each name that we typed in. Then we'll say echo for loop terminated. And exit 0. Do a control S to save, go back to the terminal and we'll type in some names, so we're gonna say for.sh and we'll type in a few names and I'll say Stacy, Tracy and Lacy. And we get hello Stacy, hello Tracy and hello Lacy. One…

内容