Operating Systems Management, Lab, 10
Linux Operating System
Lab 10 - For Loop
Instructions:
Use your VI editor to type the scripts, unless otherwise noted. Record output when needed. Do not just write the code and run it. Think about the lines and make sure you understand what the code is doing.
- Enter vi and create a file called fruits with the code below:
for fruits in apples pears oranges mangoes
do
echo $fruits are fruits
done
- Save and exit the editor
shift+zz aka ZZ
- Change the permissions on the file. Give the yourself read, write, and execute and the group and others read only. How did you do this?
chmod 744 fruits
willmcg@willmcg:~$ ls -l fruits
-rwxr--r-- 1 willmcg willmcg 75 Oct 25 14:33 fruits
- Execute the file fruits. Record the output below:
willmcg@willmcg:~$ . fruits
apples are fruits
pears are fruits
oranges are fruits
mangoes are fruits
willmcg@willmcg:~$
- At your prompt, type vi namelist (This opens your VI Editor and creates a file named: namelist)
for arg in Tom John Eric
do
echo $arg
done
- Save and exit the editor
- Make the file executable.
willmcg@willmcg:~$ vi namelist
willmcg@willmcg:~$ ls -l namelist
-rw-rw-r-- 1 willmcg willmcg 44 Oct 25 14:39 namelist
willmcg@willmcg:~$ chmod 744 namelist
willmcg@willmcg:~$ ls -l namelist
-rwxr--r-- 1 willmcg willmcg 44 Oct 25 14:39 namelist
- Run namelist (use . namelist)
willmcg@willmcg:~$ . namelist
Tom
John
Eric
- Type the following program: lab10_1
for count in one two three four five
do
echo “In the loop for $count times”
done
- Run the program and record the output:
willmcg@willmcg:~$ vi lab10_1
willmcg@willmcg:~$ . lab10_1
In the loop for one times
In the loop for two times
In the loop for three times
In the loop for four times
In the loop for five times
- Is the script above a counter? Why or why not?
it is not a counter because it is using words and not using digits
- On your own, create a for loop that will give the following output:
The Mets are a Major League Baseball Team
The Yankees are a Major League Baseball Team
The Braves are a Major League Baseball Team
The Astros are a Major League Baseball Team
Write the code below, and circle the keywords:
for team in Mets Yankees Braves Astros
do
echo "The $team are a Major League Baseball Team"
done
- Write a for loop that will run who, ls, and pwd.
for loop in who w ls pwd
do
$loop
done
you don't need echo it it will just output the same text you wrote, using $ will execute command.
Below is me working thru it. I did need ai asssitance but I understand why now.
for loop in w who ls pwd
do
echo "$loop"
done
for loop in w who ls pwd
do~~echo `$loop`~~
done
for loop in w who ls pwd ls-l
do~~$loop~~
done
willmcg@willmcg:~$ vi loop
willmcg@willmcg:~$ . loop
w
who
ls
pwdwillmcg@willmcg:~$ vi loop
[1]+ Stopped vi loop
willmcg@willmcg:~$ . loop
w
who
ls
pwdwillmcg@willmcg:~$ vi loop
willmcg@willmcg:~$ . loop
15:00:39 up 1:28, 1 user, load average: 0.36, 0.36, 0.37
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
willmcg tty2 - 13:35 1:13m 0.04s 0.04s /usr/libexec/
1000 seat0 2026-10-25 13:35
1000 tty2 2026-10-25 13:35
Brains Publiclab10_1 lab9_22 snap
Desktop Templateslab10_2 lab9_23 tmp
Documents Videoslab9_11 loop
Downloads customisolab9_19 mlb
Music fruitslab9_20 namelist
Pictures hello_worldlab9_21 protonvpn-stable-release_1.0.8_all.deb
/home/willmcg
willmcg@willmcg:~$
- What are the for loop keywords?
for in
do
done
- How do you determine how many times a for loop will run?
how many
variablesitems are in the list afterin