Friday, October 21, 2011

Useful UNIX commands



String operations (commonly used)
Equal: = (equal sign) 
Not equal: != (Not equal sign)
And: -a
Or: -o


Secure Copy
scp [local-file-path] user@host:/[remote-file-path]
scp myfiles*.jar user1@abc1234:/tmp/user1/

Secured Shell Commands
ssh user@host [unix-command] [unix-command-parameters]
ssh user1@abc1234 cp [local-file-path] [remote-file-path]
ssh userx@host123 "command-to-run-locally-1; command-to-run-locally-2"

Who all are logged into terminal
finger

To see list of all processes
top

List the file descriptors for a process id
lsof -p [process-id]

Get the length of the string
${#StringVariable}

Evaluate Mathematical/ General expression
Var1=`expr ${VAR-X} - $((${VAR-Y}*2)) - 5`
Var2=`expr substr ${TEXT1} ${Start-Index} ${End-Index}`

Secure FTP Command Sample
sftp user-x@server-y:/tmp/locationget ./abc/xyz.txt .


Read from XML Tag
function getTagValue
{
        _TAG=$( echo $( echo $1 | cut -d'<' -f2 ) | cut -d'>' -f1 )
        _OUTPUT=$( echo $( echo $1 | cut -d'>' -f2 ) | cut -d'<' -f1 )
}

function getValueFromTag
{
        # get the tag name from the text
        getTagValue $1
        case ${_TAG} in
        Target)
                _OUT1=$( echo ${_OUTPUT} | cut -d'#' -f1 )
                _OUT2=$( echo ${_OUTPUT} | cut -d'#' -f2 )
                echo "${_TAG} value1: ${_OUT1} and value2: ${_OUT2}"
        ;;
        User)
                echo "${_TAG} value: ${_OUTPUT}"
        ;;
        *)
                echo "ERROR: Incorrect TAG : ${_TAG} found!"
                exit 2
        ;;
        esac
}

getValueFromTag "new-env#right-now"
getValueFromTag "user-xxx"







No comments: