Wednesday, December 29, 2010

Best way of Escaping / in SED

If you are using SED and have the search or replace string as / then the best way to do that is use some other character as a SED delimiter instead of /. This way you will not have to escape /. It will be treated as any other character in the string.

If you are using SED as:
sed "s/old/new/"
and the old and new strings have a / somewhere in between them then use SED as below:
sed "s=old=new="
This way / becomes as another character and you don't need to escape it.

Monday, December 20, 2010

How can i get the name of Script that is running?

You can get the name of Script file that is run by simply:

echo $0 or
echo ${0}

$0 will contain the name of your shell script.

Other useful variables in Shell Script:

  • $$ = The PID number of the process executing the shell.
  • $? = Exit status variable.
  • $0 = The name of the command you used to call a program.
  • $1 = The first argument on the command line.
  • $2 = The second argument on the command line.
  • $n = The nth argument on the command line.
  • $* = All the arguments on the command line.
  • $# The number of command line arguments.
A very useful command when working with command line arguments is "shift". With this the command line arguments are shifted by certain number of places. For e.g., if you execute shift 1. $1 becomes $2 and so on....

Tuesday, December 14, 2010

How can I check the number of command line arguments passed to my bash script?

I've written a bash script auto.shI want this script to accept few command line parameters like some file names or values etc and then use them in my script somewhere. 

Sol: So here is what I found to be useful:

if test $# -lt 2 ; then
  echo "Need 2 Arguments"
  exit 0
fi 


The above basically tests how many command line arguments have been passed. You can see that here it compares it with 2. So basically there are 2 arguments that are required.

$# -> Tells Number of arguments passed

To use the arguments:

$1                                         --> First argument 
$2 and so on.....                     --> Second argument

If you simply do an echo $1 and echo $2 it will print the command line arguments that you have passed to ./auto.sh.

How can I change my login passwords in Unix/Linux?

The passwd command is used for changing Login passwords in Unix/Linux environment.


Just enter: passwd and it should come up with asking your current password and then the new one's.
Changing password for swapnil
(current) UNIX password:
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

Thursday, December 9, 2010

What are .bin files? How can I execute them?

.bin files are Linux self extracting/executing files. The only thing you need to run these type of files is to change its permission to 777.

Suppose the bin file name is myfile.bin change its permissions by following command:

chmod 777 myfile.bin

Now, to run it:

./myfile.bin

The .bin format is usually used for distributing applications. Basically they are nothing but a set of commands that run in terminal to copy, move, create, delete files etc..

Wednesday, December 8, 2010

Using SED to replace the first string found in file and ignore rest of the strings

The SED command is used to do operations line by line. Suppose you want to change first occurence of a string in file and leave other strings try the below code:

sed -e "0,/oldstringpattern/s/oldstringpattern/newstringpattern/" myfile.xml > temp

Basically the '0' tells SED which occurence need to be replaced. 0 stands for 1st occurence, 1 for 2nd and so on....