Google

Jun 8, 2014

Unix shell scripting: zip, ftp, commenting, nohup, and extracting data from a file for Java developers

Unix for software developers

1.Top 17 Unix commands Java developers use frequently 2.History commands 3.Shell scripting 4. Reading from a file
5.Purging older files 6.Splitting and archiving files 7.Emulator, SSH client 8.Unix commands for developers 9.Unix basic interview Q&A

Unix is very powerful, and you can achieve a lot with less amount of code. Most production systems run in a Unix environment, and it is very useful to know your commands to solve technical issues.

Q. How will you zip a data file mydata.dat?
A.

gzip -f ${DATAPATH}/mydata.dat

the output file will be mydata.dat.gz

You can unzip it

gunzip ${DATAPATH}/mydata.dat.gz

the output will be mydata.dat


Q. How will you securely send the files mydata.dat.gz and mydata.dat to another server?
A. secured FTP. That is SFTP (Secured File Transfer Protocol).

sftp ${LANDINGUSER}@${LANDINGSERVER}  << EndHere >> ${LOGFILE}
   cd  ${LANDINGAREA}
   put ${DATAPATH}/mydata.dat.gz
   put ${DATAPATH}/mydata.dat
   quit
EndHere


Q. How will yo block comment the above FTP ing?
A. Single line comment is "#" and a block comment is

: <<'END'
comments here
and here
END


FTP block is commented as

: <<'END'
sftp ${LANDINGUSER}@${LANDINGSERVER}  << EndHere >> ${LOGFILE}
   cd  ${LANDINGAREA}
   put ${DATAPATH}/mydata.dat.gz
   put ${DATAPATH}/mydata.dat
   quit
EndHere
END


Q. How will you go about executing commands after you exit from a shell prompt?
A. If you are not sure when the job will finish, then it is better to leave job running in background. But, if you log out of the system, the job will be stopped and terminated by your shell. To keep the job running even after you have logged out you can use the nohup  command. "&" is for running in the background.


nohup find . -name *.dat &
nohup ./myscript.ksh &

The execution output will be logged in nohup.out file.


Q. How will you display the contents of a file named myapp_last_run_date.dat?
A. cat command to display

$ cat myapp_last_run_date.dat


Output is say:

|         May 21 2014 12:00AM |




Q. How will you extract out the date from the above format to a shell variable?
A.

Step 1: get the first line.

$ cat myapp_last_run_date.dat | head -1

outputs first line of the file "myapp_last_run_date.dat "

|       May 21 2014 12:00AM |


Step 2: Trim all the white spaces with the sed command

$ cat myapp_last_run_date.dat | head -1 | sed -e 's/ *| */|/g'

outputs

|May 21 2014 12:00AM|

Step 3: Remove the last pipe with regex "|$". "-e" means expression,

cat myapp_last_run_date.dat | head -1 | sed -e 's/ *| */|/g' -e 's/\|$//'

outputs

|May 21 2014 12:00AM

Step 4: Remove the starting pipe with regex "^|". "g" means global change

cat myapp_last_run_date.dat | head -1 | sed -e 's/ *| */|/g' -e 's/\|$//' -e 's/^|//g'

outputs

May 21 2014 12:00AM

Step 5: to break the commands in multiple lines, use "\"

cat myapp_last_run_date.dat | head -1 | \
   sed -e 's/ *| */|/g' -e 's/\|$//' \
       -e 's/^|//g'

outputs

May 21 2014 12:00AM

Step 6: to assign the date to a variable in Korn shell (ksh).

export LAST_RUN_DATE=`cat myapp_last_run_date.dat | head -1 |\
                         sed -e 's/ *| */|/g' -e 's/\|$//' \
         -e 's/^|//g'`;


Step 7: to check if the variable is set

echo $LAST_RUN_DATE



More Unix tutorials like this for Java developers:


Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home