Wednesday 14 November 2012

Get a file from a directory and assign it to variable

 At the bash shell prompt of Linux:

var=$(ls /home/user)

echo $var

this should echo the content of var, which is the listing of /home/user

You may want to assign to a variable the result of a find command:

var2=$(find /home/user -type f -name "MyFile*")

echo $var2

this should list the content of var2 - all files with name starting with "MyFile" in the /home/user path.

Get current directory path

CURRENTDIR=`pwd`
echo $CURRENTDIR
#will print the current directory

Note: note the ` char.

Functions in Linux

Function need to be declared FIRST before it is used.

functionName(){
    if [ "$1" == "1" ]; then
         echo "paramether is 1"
   else
       echo "parameter is not 1"
    fi
    if [ "$2" != "" ]; then
       echo "there is second parameter"
    fi
}

and call it like this:

functionName

OR pass an argument:

functionName 1

OR more arguments:

functionName 1 aString