Tag: Unix

How to run a background command in script as a parameter in unix shell scripts

by on Jun.21, 2013, under Programming, Unix

Many times you would have tried running a command or script directly in a unix script by providing the name and path of the script. Also you would have run scripts are commands by assingning to variables. But by any chance if you had tried running a script or command assingned to variables you might got struck and which also gives you a vauge error message.

For example consider a script param.sh as below

echo $1

And you are running the scipt as

./param.sh 'ls -ltr &'

You would be getting a error message

./& not found

This issue can be resloved by using eval command in param.sh and the new script looks as below.

eval $1

eval command will take an argument and construct a command of it, which will be executed by the shell. It is an inbuit command and there is the man page http://www.unix.com/man-page/posix/1posix/eval/

Leave a Comment :, , , more...

Splitting file based on line numbers in UNIX

by on Aug.22, 2009, under Unix

This script will be useful if you require to split a huge file based on number of lines or records. Normal file splitters available in the market split the file based on the size (byte, KB, MB) which cannot be used to split based on number of lines or records.

Steps to use the script:

  1. Save the below script as lsplit.ksh
    while read line
    startLineNo=`echo $line | cut -f1 -d,`
    endLineNo=`echo $line | cut -f2 -d,`
    if [ "$endLineNo" != "" -a "$startLineNo" != "" ]; then
    echo "Cut here from $startLineNo to $endLineNo"
    sed -n "$startLineNo","$endLineNo"p $inpFile > $inpFile.split.$count
    count=`expr $count + 1`
    done < $propFile
  2. Create a properties file SSNRange.txt.prop which would contain the range of records or lines. Example of properties file is as follows

    1505, 7000

  3. Run the script
    $ lsplit.ksh infile.txt

  4. Three output files will be created
    • infile.txt.split.1 –> Creates a file with first 400 lines
    • infile.txt.split.2 –> Creates a file with lines starting from 401 to 1504
    • infile.txt.split.3 –> Creates a file with lines starting from 1505 to 7000

Advantages of this script:

  • File is split based on line numbers are records.
  • No manual editing is required the correct the first and last records
  • Easy to handle it in batch

Courtesy: Santhosh Fabian

Leave a Comment :, , , more...

Closing telnet session without terminating the foreground process

by on Nov.04, 2008, under Unix

Finally have found out a way to terminate the telnet session with out killing the foreground process which is running. Anyone struck with the above situation can follow this 🙂

Assume a script “script1” is running in the foreground for a long time and this script needs to be retained even if the telnet session is closed. Here are the steps which needs to be followed.

  1. On the telnet screen press <CTRL + Z>. This will temporarily stop the script or process to run.
    $ ./script1.ksh
    [1] + Stopped (SIGTSTP)        ./script1.ksh
  2. Type in the command “bg” to run the process in background
    $ bg
    [1]     ./script1.ksh&
  3. Identify the session process ID. This can be done by giving the “ps” command in the prompt.
    $  ps
    PID      TTY   TIME  CMD
    4882522  pts/7  0:00 ps
    4984988  pts/7  0:00 -sh
  4. Identify the PID of the script which is is shifted to background. In our case “script1” This can be done by ps command piped with grep as below.
    $ ps -ef | grep script1.ksh
    user1 4399240 4984988   0 08:57:49  pts/7  0:00 grep script1.ksh
    user1 5029960 4829226   0 07:57:23      -  0:00 /usr/lpp/ars/bin/script1.ksh
  5. Now with all the PID’s collected, use “nohup” command
    $ nohup -p 4984988
    $ nohup -p 5029960

This will make the script or process not to be terminated even if the telnet session is closed.

NB: The telnet session process 4984988, will be running at the background unless it is killed.

Courtesy: Santhosh Fabian

6 Comments :, more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!