Distributed Systems : Bash examples

Iterating over files containing string ukko

for filename in `ls|grep ukko`
do
    Do something here
done

Itering over all lines in a file called test.txt

for line in `cat test.txt`
do
    Do something here
done

 

Get last three characters of a string called myString

lastThree=${myString:(-3)}

 

Sending and receiving strings using netcat

echo "bonjour"|nc myHost.mydomain.com 30000 
msg=`nc -l 30000`

Get process info of jsmith's process called myBestestProgram.sh

ps -u jsmith|grep myBestestProgram.sh

 

Get a number between 1-10
somewhatRandomNumber=$(shuf -i 1-10 -n 1)

 

Redirecting output to /dev/null
./annoyingOutput.sh 1>/dev/null 2>/dev/null