This lab was an introduction to Bash scripting and linux processes.
pskill
to kill the process.
#!/bin/bash
javac Loop.java
java Loop
#!/bin/bash
# if statement example
if [ ... ] then
echo "Hello world"
exit 1
fi
# case statement example
case $a in
[0-9]*) ;;
*) echo "Arguments must be numbers.">&; exit 1;;
esac
This all in the end culminated in writing a bash script that would only take 2 single digit numbers, outputting "OK" and "Error" otherwise. That can be found here (should that work).for a in $*
do
echo "$a"
done
ps
will list all processes associated with the user (ps -e
for everything).
ps | grep <process>
will find processes by that name and print them out in the ps table. Using pgrep
will only print out the PIDs and nothing else.
pkill
will kill a process based on name (though bear in mind for applications with multiple processes this can not entirely work), whilst kill
takes a PID.
./runloop &
.
$
, if and case statements are closed by writing if and case backwards (fi
and esac
) respectively, and for loops are closed by done
.
Whilst still followable, this is where the content started increasing in difficulty. First, the processes section was not too hard to get, along with piping outputs from one command to another, however this was when I was first met with the horrible, convoluted, unintuive syntax and style of the bash script. Whitespace rules were incredibly strict, stricter even than a whitespace dependent language like Python, and in ways that were hard to grasp. Examples include things like do, then
having to be on a new line to their respective for, if
statements, as well as certain things throwing errors if there are no spaces surrounding them: if [ cond ]
good, if [cond]
bad.
Bash seems to be a language where "if it works it works", and I need to get used to its convoluted syntax, just enough to make a script work.