Tuesday, August 21, 2012

How could you debug specific lines in shell script intelligently?

We can implement an intelligent DEBUG-function to debug a script at specified line by putting the following function at the beginning of the script:

_DEBUG="on"
function DEBUG()
{
 [ "$_DEBUG" == "on" ] &&  $@
}

When done with debugging  or before moving your script to production, set "_DEBUG" to 'off'.
 _DEBUG="off"  # set to anything but not to 'on'

For Example:

#!/bin/bash

_DEBUG="on"
function DEBUG()
{
[ "$_DEBUG" == "on" ] && $@
}

DEBUG echo 'Hi!'

How do you convert all cvs files to text or change the extension of all files?

#!/bin/sh

for FILE in *.cvs
do
 #Strip off extension
 FNAME=`echo $FILE | awk -F . '{ print $1 }'`
 mv $FILE ${FNAME}.txt
done

How do you get last command-line parameter passed to shell script?

#!/bin/sh

echo "${!#}"

How do you rename all the files in a directory at terminal?

for FILE in *; do mv $FILE $FILE.bkp; done
or
ls *.* | xargs -i mv \{\} \{\}.bkp

Monday, August 13, 2012

How do you get/print last field of every input line in UNIX?

sed -e 's/^.* //' <File Name>
or
awk '{ print $NF }' <File Name>

How do you implement auto-login FTP process in Perl via a proxy (.netrc)?

#!/usr/bin/perl -w

use Net::FTP;
use Net::Netrc;
use Logger::Simple;

# Declare and Define values for variables
my $logfile="/home/logs/$0.log";
my $log=Logger::Simple->new(LOG=>$logfile);

# Get the login info from the .netrc file located in HOME directory
my $RemoteHost=Net::Netrc->lookup('<Server Name>');
my $User=$NT->login;
my $Pass=$NT->password;

# Craete the FTP object
my $ftp=new Net::FTP($RemoteHost, Debug => 1);

# Log into Server and list files
if(! $ftp->login($User,$Pass) )
{
  $log->write("Unable to connect to $RemoteHost!");
  die $ftp->message;
}
else
{
  $log->write("Successfully Connected to $RemoteHost");
  $ftp->ascii();
  $ftp->cwd("/HOME/nayakr/project/perl");
  @list=$ftp->ls('-lart');
  $ftp->close();
}
foreach (@list)
{
        print $_;
}

Wednesday, August 8, 2012

How could you display the last line of a file in UNIX?

tail -1 <File Name>
or
sed '$!d' <File Name>
or
awk 'END{print $0}' <File Name>

What is the difference between 'MV' and 'CP' command?

mv :
- used to rename a file or move a file from one location to other.
- doesn't change timestamp.
- works like Cut+Paste.

cp :
- used to copy a file or create a duplicate of a file
- modifies the timestamp.
- works like Copy+Paste.

How could you read/display the nth line of a file?

head -n <File Name> | tail -1
or
sed 'nq;d' <File Name>
or
awk 'NR==<Line No.> { print $0; exit }' <File Name>

How do you set variable accessible form sub shells?

This can be achieved by using "export" command. It marks the variable for automatic export to the environment of subsequently executed commands i.e. makes the local shell variable global or environmental. For example :

export PATH=$PATH:/usr/local/bin

will make PATH variable global.

To see the list of all exported variables and functions, type :

export -p