Showing posts with label shell. Show all posts
Showing posts with label shell. Show all posts

Monday, February 16, 2015

What is /dev/null?

I've used /dev/null for two purposes -
1. to discard output of a command
2. to suppress any potential error messages from a command

You may already know that you can redirect the output to a
1. file using greater than symbol ">" and 
2. command using pipe symbol "|"

Redirection to /dev/null works just as if the redirection was to a file. The only exception that the output cannot be retrieved.

Before we dive in, it's important to know that standard output is represented as 1 and standard error as 2.


ls -1 c* 2> /dev/null  #redirects the standard error (2)
ls -1 c* &> /dev/null  #redirects both the stderr (2) and stdout(1)
                              #variations include 2>&1, >>
ls -1 c* 1> /dev/null  #redirects the std output (1)
                          #variation is >

Here's an example to show an application of /dev/null. Run the code below to see how differently they behave.

if test ! -z "`ls -1 y* 2> /dev/null`"  ; then
   echo "Files starting with y exist."
else
   echo "Files starting with y DO NOT exist."
fi

if test ! -z "`ls -1 x* `"  ; then
   echo "Files starting with x exist."
else
   echo "Files starting with x DO NOT exist."
fi


To think about -
1. What happens when you do > filename?
2. What happens with you do > instead of 1> in the first example?
3. What does  this do? ls -1 c* 2> logfile > file.lst

Wednesday, December 14, 2011

Using Shell Variables in awk and sed

For sed - use double quotes around the variable.

sed '/'"$pattern"'/d' filename
Another option is to enclose entire sed command in double quotes.

sed "/$pattern/d" filename

For awk - use combination of double quotes single quotes double quotes

awk '{ if ($(1) == "'"$pattern"'" ) {print $2}}'
Optionally, you could create an awk variable from shell variable.

awk '{ if ($(1) == ipattern ) {print $2}}' ipattern=$pattern filename

Wednesday, June 22, 2011

BASH shell only - brace expansion

You might have used brace expansion if you have tried this very popular way of backing up files - 
cp filename{,.bk}

The command above will copy filename to filename.bk. It saves you from writing the filename again. 
This neat stuff is not compatible with korn shell.
Some other examples to reinforce how this works.

echo John{Doe,Smith}
  JohnDoe JohnSmith
echo {John,Jane}{Doe,Smith}
  JohnDoe JohnSmith JaneDoe JaneSmith
  

Note the lack of space after comma.

Monday, May 9, 2011

Shell script for multiple SED edits

Following shell script
1.    Reads the names of the files to be edited from the file "filelist".
2.    Executes two sed statements on each of those files and
3.    Overwrites the original file. 


#!/usr/bin
while read line
do
sed 's/0512/0505/g' $line > temp
sed 's/05\/13/05\/06/g' temp > $line
done < filelist 

Thursday, January 27, 2011

Shell Variables are Global by Default

All variables in a shell script are global unless specifically declared as local. Local variables can be used only inside a function.

#!/bin/bash
# Global and local variables

carwash()
{
local vin="17DIGITS"
echo "IN Carwash. Your windscreen is:" $1 "and your VIN is:" $vin
post_carwash_status="BEARY CLEAN"
}

status="DIRTY"
carwash $status
echo "After carwash. Your windscreen is:" $status "and your VIN: " $vin
echo "Post Carwash Status: " $post_carwash_status

OUTPUT

IN Carwash. Your windscreen is: DIRTY and your VIN is: 17DIGITS
After carwash. Your windscreen is: DIRTY and your VIN: 
Post Carwash Status:  BEARY CLEAN
Notice that the local variable vin is not available outside the function.

ps to list only ppid

I generally use ps to get ppid to kill a process and finally figured a way to list just that.

ps -o ppid
And to list ppid without header, indicate that it should have a value. To display more information, add the headings preceded by output formatter -o.

ps -o ppid=
ps -o ppid= args= | grep Firefox 

Thursday, October 28, 2010

When ssh relentlessly keeps asking for password

Check if your from and to home directories plus ssh directory have the correct permissions. All the details here -

Thursday, September 9, 2010

list directories

Listing directories in UNIX korn shell
$ls -d */
Mail/ sourcefiles/ mib/ dir3/
awc/ locks/ targetfiles/ dir4/
bin/ log/ perlo/ z12/

Wednesday, April 28, 2010

Function in shell script

Declare the function before calling it.# Function to get password
functionGetpwd()
{
echo "Please provide $USR database password"
read PWORD
echo "Connecting to Database"
}

Call the function
 functionGetpwd

Wednesday, May 27, 2009

Using translate function on Unix in a shell script


echo "TRansLate" | tr [:lower:] [:upper:]

echo "Ok to continue? y or n"
read respons
if [ `echo ${respons} | tr [:lower:] [:upper:]` = "Y" ] ||
[ `echo ${respons} | tr [:lower:] [:upper:]` = "YES" ] ; then
echo "Processing..."
else
echo "Exiting"
exit 1
fi


On executing the script

/home/ruchi> ./test
TRANSLATE
Ok to continue? y or n
yes
Processing...