torstai 10. joulukuuta 2015

Shellscripting like a boss

It is good idea to put all your shell script commands in a directory and export it to the path. In order to do this open up your .bashrc file of .profile

nano ~/.bashrc

Add this line to it 

export PATH=$PATH:~/scripts

It means in your home folder there should be a folder named scripts. Like /home/username/scripts.

Then reload your bashrc file 

source ~/.bashrc

And now you are ready for scripting. Add new file named hello to the script folder. 

Add this

#!/bin/bash 
echo My first program

Then in any directory type hello and your script executes.

Adding parameters to script:

#!/bin/bash

function echoshit() {
    echo "No parameter found or wrong parameter"
}

if [[ $1 == --trolli ]];
then
  echo "trolli echoed"
else
  echoshit;
fi


Read more: http://www.bashguru.com/2009/11/how-to-pass-arguments-to-shell-script.html