CSCM98 Lab Class #5 Getting familiar with the command line Lab Class Description. Unix has some powerful commands that can be combined inside shell scripts. Today we will have a look at various commands before seeing at how shell programming works. Firstly, log on the Unix computers and start a command line. Note that a few of those commands may not work on your account/system. Task 1 First, create a directory for the lab class using the following commands: cd cd ~ mkdir CSCM98_lab cd CSCM98_lab You will then need to download the lab file from the course website and add it to this directory (lab5.tar.gz). Then, uncompress the files with the following command from the command line. Make sure you download the file in the right directory! tar xvf lab5.tar.gz then try unzip lab.zip Task 2: Simple commands Try the following commands from the command line and describe their action ls ls -la pwd whoami groups
who w finger myusername mail myusername@swansea.ac.uk Finish with a. on the last line wc lab1.cpp more lab1.cpp cat lab1.cpp diff lab1.cpp lab2.cpp man kill followed by Q man kill more use spacebar du lab1.cpp df date grep main lab1.cpp
find. name "lab.zip" ps ps -edf pstree kate followed by ctrl-c in the command line kate& top find the kate pid from top and follow with ctrl-c kill 9 kate_pid whereis ls cmp lab1.cpp lab2.cpp man chown env
Task 3: Pipes and text files Try the following commands in the following order and ask if necessary: echo helloworld echo ls>myls ls -la ls la grep myls./myls man chmod man umask man chown man chgrp chmod u+x myls./myls echo "echo Hello world" >>myls./myls man cut man tr ps -edf grep mysusername tr s " " cut d' ' f1,2 ls *.cpp more man cron
Task 4: small introduction to shell scripts cat>script.sh then enter the following: for x in lab1.cpp 1 5 do echo hi $x! done mkdir tmp cd tmp pwd ctrl-c chmod the script file with the right properties, then execute it. Test the command pwd again. Finally, lookup eval Online as this command will be useful in our next exercise. Task 5 Read, create (by copy-pasting text possibly) and run the following bash script: #This is a comment #!/bin/bash echo Calculator polish notation echo enter. for exiting sum=0 lastnb=0 echo $lastnb while read entry do case "$entry" in.) break +) let sum=$(($sum+$lastnb)) -) let sum=$(($sum-$lastnb)) /) let sum=$(($sum/$lastnb)) "*") let sum=$(($sum*$lastnb)) *) let lastnb=$entry esac done Try to understand every line of code and ask if needed. To run a command, the script must be executable (an x should show up for your file after an ls -la command.) Use the command
chmod u+x on your script first before execution. Task 6: The script below is trying to count how much space is taken by files including $1 and greater than $2 (E.g. spacescript ".cpp" 50 ). The script does not however work. This is due to the fact that the while loop is included in a pipe (that creates a new context for the process) and the global sum is therefore not updated at the end. Try to run the code first. In a second time, modify the script by 1) Redirecting the ls line to a temporary file using > ; 2) Using both the created file and < after the while loop to provide the input lines to the while loop; 3) Finally deleting the temporary file. #!/bin/bash echo sum of large files sum=0 ls -la grep $1 tr -s " " cut -d' ' -f5 while read entry do if [ $entry -gt $2 ] then echo File size: $entry sum=$(($sum+$entry)) ; fi done echo Total usage for $1 $sum Task 7: Create a script that computes a Fibonacci value recursively. i.e. the script should call itself. Do not try with large values. Note that arithmetic can be written the following way: let val1=$myvariable+1 or val1=$(($myvariable+1)) while getting the ouput of a command in a variable can be written as follow for instance: result=$(./myscript myargument)
Others: System, admin commands and more (optional) man dump man restore man badblocks man lsmod man insmod man rmmod man modprob man quota man ldd man watch man useradd man userdel man usermod man groupmod man newgrp man id man lid logname su l alogin man sudo man passwd man ac last tset r uname a arch lastlog lsof more strace man nc ping Swansea.ac.uk free man stat vmstat man dmesg hostname hosted sar man realdelf man size man logger man logrotate man pidof man nice man halt math shutdown man reboot man service man nmap ifconfig netstat man mount man umount
man mkswap man mke2fs man mkbootdisk man ulimit man strip man nm man xrandr man linx man wget