The "read" command


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users The "read" command
# 1  
Old 11-12-2003
CPU & Memory The "read" command

Hello there.

I'm trying do make a script capable of verifying if a given script is running at the moment in the environment, and if does, forbid it to execute.

So, I've coded this so far:
Code:
#!/bin/ksh
set CUR_SHELL="/bin/ksh"
set V1="VAR1"
set V2="VAR2"

ps -e -o args | grep "$CUR_SHELL $1" | tr -s " " | cut -d" " -f1 | 
while read -r V1 V2
do
    if [ "$V1" = "$CUR_SHELL" ]; then
        echo "Error - Program is already running."
        echo "V1 -> $V1"
        exit 1
    fi 
done
echo "V1 -> $V1"
echo "V2 -> $V2"

if [ -z "$V1" ]; then
    echo "V1 IS NULL"
fi

I was trying to encapsulate the ps and its pipes into a variable, then to use it like
while read $COMMAND V1 V2 (the second variable exists only for testing purposes)

or while read ....
do

done < $COMMAND

But I got into a endless loop.
Also, I got into the first if and the variable V1 printed out was displaying NULL...

Does anyone have any suggestion, tip or corrections to this script ?

Thanks in advance,
435 Gavea - bRaZiL - thE hElL iS herE !!! Smilie

added code tags for readability --oombera

Last edited by oombera; 02-21-2004 at 02:18 AM..
# 2  
Old 11-12-2003
I had created a function awhile back to create a lock file based upon the process number of the shell. The function takes two arguments, one for the name of the log file and the other is $$. The script creates a lock file in the /tmp directory. This file holds the process ID of the shell that created it. If you attempt to execute the script while another instance was running, this function will prevent the second instance from running. Works well for me. Feel free to use if you so choose.
Code:
CreateLockFile () {

unset PROCESS_ID
unset ID
LFILE=${TEMP_PATH}/${1}_${TODAYS_DATE}.LCK
ID=$2

if [ -r ${LFILE} ]
 then
    OPID=`cat ${LFILE}` 2> /dev/null
    if [ -z ${OPID} -eq 0 ] #Make Sure OPID contains a value
     then
	 exit ${FAILURE} "ERROR-APP-->: `basename ${LFILE}` exists but contains no Process ID" | tee -a ${INLOG}
     else
        PROCESS_ID=`ps -p ${OPID} | grep ADD-SCRIPT-NAME-HERE | awk -F" " '{print $1}'  2> /dev/null`

        if [ ${PROCESS_ID} ]  #Lock File is there, check if process is actually running
         then
           echo "WARNING-->: ${1} Script Is Currently Running [PID=${OPID}], Exiting. ${DATE_TIME}" | tee -a ${INLOG}
           exit ${SUCCESS}
        else
	  echo "INFO-->: Old Lock File with PID= [ ${OPID} ] Exists But Process Is Not Running. " >> ${INLOG}
	  echo "INFO-->: Overwriting Old PID with New PID Value of [ ${ID} ] " >> ${INLOG}
          echo "$ID" > ${LFILE}
        fi 
     fi
else
  echo "$ID" > ${LFILE}

    if [ $? -ne 0 ]
      then 
        exit ${FAILURE} "ERROR-APP-->: Could Not Create Lock File - Exiting " | tee -a ${INLOG}
    fi
fi
}

added code tags for readability --oombera

Last edited by oombera; 02-21-2004 at 02:19 AM..
# 3  
Old 11-12-2003
Don't do:
set var="string"
in ksh. It's just
var="string"
# 4  
Old 11-13-2003
Quote:
Originally posted by Perderabo
Don't do:
set var="string"
in ksh. It's just
var="string"
Using set doesn't make the code more readable ?
# 5  
Old 11-13-2003
Thanks, google

Well, I've heard about some solutions using a temporary file as well, but
my "boss" said to avoid this option if I can.

So I'm trying to figure out a different solution...
But I didn't thaught about using the PID ... it may be a good point...

Thanks anyway !
# 6  
Old 11-13-2003
Quote:
Originally posted by 435 Gavea
Using set doesn't make the code more readable ?
Using set makes the code wrong.
var="string"
will set $var to the value "string". On the other hand,
set var=string
does not affect a variable called var. Instead it clobbers the arguments and sets $1 to the value "var=string". Try it,
set var="string"
echo $var
echo $1
# 7  
Old 11-13-2003
MySQL Interesting !

So...
What's actually the purpose of the set command ?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

2. Shell Programming and Scripting

Read from "list1" and list matches in "list2"

I want to print any matching IP addresse in List1 with List 2; List 1 List of IP addresses; 161.85.58.210 250.57.15.129 217.23.162.249 74.76.129.101 30.221.177.237 3.147.200.59 170.58.142.64 127.65.109.33 150.167.242.146 223.3.20.186 25.181.180.99 2.55.199.32 (3 Replies)
Discussion started by: lewk
3 Replies

3. Shell Programming and Scripting

The "read" command misinterprets file names containing spaces

The "read" command, which is built into bash, takes words from the standard input. However, "read" is not good at taking file names if the file names contain spaces. I would like my bash script to ask the user to enter file names, which may contain spaces. Can you think about any technique for... (14 Replies)
Discussion started by: LessNux
14 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. Shell Programming and Scripting

Remote script skips "read" command

This script is supposed to display a file ( crontab ), ask the user if they wish to update the file, then it goes through an update routine. #!/bin/bash FILE=/etc/crontab tail -5 $FILE echo -n "Does crontab need updating" read HOURS ...routines ....etc... Runs locally... (8 Replies)
Discussion started by: Bubnoff
8 Replies

6. Shell Programming and Scripting

Manipulating input into the shell "read" command

Hi All I have a migration program that creates directories based on dates, e.g 20090714 20090812 etc.. Based on their requirements, the user will select the directory they want to perform an action on. Currently, this is a snippet of the code I use no_of_versions=`ls | wc -l` if... (2 Replies)
Discussion started by: kingpin2502
2 Replies

7. Shell Programming and Scripting

"read" command ignoring leading spaces

I have to read a file line by line, change it and then update the file. Problem is, when i read the file, "read" command ignores leading spaces. The file is a script which is indented in many places for clarity. How to i make "read" command read leading spaces as well. (3 Replies)
Discussion started by: vickylife
3 Replies

8. Shell Programming and Scripting

read -p "prompt text" foo say "read: bad option(s)" in Bourne-Shell

Hallo, i need a Prompting read in my script: read -p "Enter your command: " command But i always get this Error: -p: is not an identifier When I run these in c-shell i get this error /usr/bin/read: read: bad option(s) How can I use a Prompt in the read command? (9 Replies)
Discussion started by: wiseguy
9 Replies

9. Shell Programming and Scripting

Breaking input with "read" command

In this post, Perderabo's script says echo 05/06/25 14:15:56 | IFS=" /:" read Y1 M1 D1 h1 m1 s1 which, if I am not wrong, will break the input into Y1, M1 et al. I tried the following in my code #! /bin/ksh # per.sh typeset -R2 HOUR=00 typeset -R2 MIN=00 typeset -R2 SEC=00 ... (2 Replies)
Discussion started by: vino
2 Replies

10. Shell Programming and Scripting

how to request a "read" or "delivered" receipt for mails

Dears, I've written a script which allows me to send mails in different formats with different attaches. Now I still want to add a feature to this script. My users would like to be able to receive a "read" or "delivered" receipt for their mails. The script send mails on behalve of an specific... (1 Reply)
Discussion started by: plelie2
1 Replies
Login or Register to Ask a Question