Distributed Systems : Bash Examples

Looping through a file structure and executing a command read from /INPUT file over ssh to a certain Ukko node and writing output to /OUTPUT file

for ukkonode in `ls|grep ukko`
do
    thisDirectory=`pwd`
    commandToExecute=`cat $ukkonode"/INPUT"`
    ssh $ukkonode".hpc.cs.helsinki.fi" $commandToExecute '>'$thisDirectory'/'$ukkonode'/OUTPUT' &
done

 

Simple for loop with an if clause

for x in {1..5}
do
    if [ $x -eq 4 ]
    then
        echo "Hello world!"
    fi
done

 

Iterating over files where the filename contains the 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