Friday 26 October 2012

Put file content into a variable

Linux

filetext=`cat printme`
echo "$filetext"

NOTE: note the ` char.
This will read the file printme and will assign its text to the variable filetext

"IF" on Linux shell script


VALID_PASSWORD="secret" #this is our password.

echo "Please enter the password:"
read PASSWORD

if [ "$PASSWORD" == "$VALID_PASSWORD" ]; then
 echo "You have access!"
else
 echo "ACCESS DENIED!"
fi
 
 
Comparisons:
-eqequal to
-nenot equal to
-ltless than
-leless than or equal to
-gtgreater than
-gegreater than or equal to

File Operations:
-sfile exists and is not empty
-ffile exists and is not a directory
-ddirectory exists
-xfile is executable
-wfile is writable
-rfile is readable
-ntests to see if the argument is non empty   

X=""
# -n tests to see if the argument is non empty
if [ -n $X ]; then  
 echo "the variable X is not the empty string"
fi

if [ "$AGE" -lt 20 ] || [ "$AGE" -ge 50 ]; then
 echo "Sorry, you are out of the age range."
elif [ "$AGE" -ge 20 ] && [ "$AGE" -lt 30 ]; then
 echo "You are in your 20s"
fi 

if [[ $count -gt 0  &&  $somevar != $var ]]; then
...no brackets inside, only double [[  ]]
fi



if [ $count -gt 0 ] && [ $somevar != $var ]; then
...do something
fi


Thursday 25 October 2012

File/Directory transfer between Linux machines

scp user@IP:/path/to/file   /copy/it/here

scp -r user@IP:/path/to/dir  .     -> this will copy recursively (-r) the directory to the current directory of the local machine

Wednesday 24 October 2012

File transfer between Linux machines - scp

To copy a file from another machine:

# scp username@123.123.123.123:/get/this/file   /put/it/here

to copy a directory
# scp -r username@123.123.123.123:/get/this/file   /put/it/here
-r
Recursive, so it copies the contents of the source-file (directory in this case) recursively

Unzip expects -d /path/to/unzip/dir

Shell command "unzip" turned out to require paramether -d /path/to/unzip/dir.
Otherwise it will presume you want to unzip it in the current directory.

So the format should be:

unzip /my/zipfile.zip -d /path/to/unzip

I had tried

unzip /my/zipfile.zip  /path/to/unzip  and it did not work, so paramether -d was required.

Moving file between Windows and Linux change the EOL characters

I needed to commit a shell script to the Jenkins CI.
So first moved the script from the Linux machine to my local working copy (on Windows) and then commited it to the SVN. It turned out I could not execute the script after checkout on a Linux machine and reason was the the file transfer from Linux to Windows changed the EOL (End Of Line) endings to Windows format (CR/LF) replacing the LF format of UNIX.

To fix it, open script in Notepad++ -> Edit -> EOL conversion -> UNIX format

Then commit and should work after being checkout on Linux machine.

Also, I had tried to change encodings to whatever possible in Notepad++ and left it to Unicode-8. Again, that was a reason script to not be possible to run on Linux. So, change Encoding to "ASCII" by "Convert to ASCII"