Sponsored Content
Full Discussion: code checking
Top Forums Shell Programming and Scripting code checking Post 302558201 by maverick72 on Friday 23rd of September 2011 05:05:18 AM
Old 09-23-2011
Code:
# prgname='test.sh' # cat $prgname sleep 120 # sh test.sh  &                    [1]     25806

That part is telling ya to assign the script name to the variable prgname, what it does (the cat part) and he's executing it in background (&). You can see the process id of the script at the end (25806).

He's just showing you that the script is already running so you can test it.

Code:
# if [ "$(ps -ef|awk '/'${prgname}'/ && !/awk/')." != "." ] > then > echo "${prgname} running !" > fi test.sh running !

That part is the actual loop thats looking for the running script. [ ] Tests if the script is already running. If it does it echo's "(script name) running!". The last part is the actualy the result of the command which tells you ... yeah your script is running.

Back a bit... the test part.

[ ] this is the test part. So if [test] is true do something.

Test what? If the variable exist. If it exists you want to know.

ps -ef will list the processes (good thing to know if your looking for a process).

He's pipping it into a awk which will search for the variable prgname BUT will leave out awk. You always want to do this cause else it will always return with your actual search. See ex:

Code:
$ ps -ef | awk '/'ssh-agent'/'
user     1625 25793  0 10:55 pts/2    00:00:00 awk /ssh-agent/
user    20491     1  0 Sep19 ?        00:00:00 ssh-agent

You want the second one but not the first one. So the AND operator (&&) is really needed here to skip your search.

Ok ... last thing he does ... he assigns the result to a variable PLUS a dot "$(bla bla bla bla)."

See the trailling dot at the end? Thats actually needed incase your result is empty. If it is your variable will be a simple dot. That simple dot will be compared the other part of the test "."

That != says NOT equal so if it finds your script name that test will look like:

test.sh. (see the dot at the end of test.sh) NOT equal to dot

That test is true so do somehing.

In case its not running the test will look like this:

"." NOT equal to "." which is not true so it will skip the "do something part"

Hope it helped. Thats really the simplest i can do.
 

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Code checking for all values in the same if statement.

I am trying to set up a variable based on the name of the file. function script_name { if then job_name='MONITOR' return job_name; elsif then job_name='VERSION' return job_name fi } for i in `ls *log` do script_name $i done. (4 Replies)
Discussion started by: oracle8
4 Replies

2. Shell Programming and Scripting

Error code checking

I'm trying to create a directory from my Perl script. Only if the there was an error I want to let the user know about it. So if the folder exists is ok. This is what I think should work: `mkdir log 2>/dev/null`; if($? == 0 || $? == errorCodeForFileExists) { everyting is fine } else {... (3 Replies)
Discussion started by: jepombar
3 Replies

3. Shell Programming and Scripting

checking the return code

hi i have a file, i am reading line by line and checking a line contains a string , `grep "Change state" $LINE` if then echo "The line contains---" else echo "The line does not contains---" i need to check the return code , but i am getting an error ... (4 Replies)
Discussion started by: Satyak
4 Replies

4. Programming

code for checking username char is not present in password.

hi friends , i need c code for my app, in my app the user can create the new user for the system, while creating the new user ,i try to include the condition the user name is not present in password. my exe requrment is : the user name char is not present in password,if the password... (6 Replies)
Discussion started by: vasu28
6 Replies

5. Shell Programming and Scripting

Checking for duplicate code

I have a short line of code that checks very rudimentary for duplicate code: sort myfile.cpp | uniq -c | grep -v "^.*1 " | grep -v "}" It sorts the file, counts occurrences of each line, removes single occurrences and removes the ubiquitous closing brace. The language is C++, but is easily... (3 Replies)
Discussion started by: figaro
3 Replies

6. SCO

Stop boot system at "Checking protected password and checking subsystem databases"

Hi, (i'm sorry for my english) I'm a problem on boot sco unix 5.0.5 open server. this stop at "Checking protected password and checking subsystem databases" (See this image ) I'm try this: 1) http://www.digipedia.pl/usenet/thread/50/37093/#post37094 2) SCO: SCO Unix - Server hangs... (9 Replies)
Discussion started by: buji
9 Replies

7. Shell Programming and Scripting

Checking LB status.. stuck in script syntax of code

#!/bin/ksh #This script will check status of load balancer in AIX servers from hopbox #Steps to do as folows : #Login to server #netstat -ani | grep <IP> #check if the output contains either lo0 OR en0 #if the above condition matches, validation looks good #else, send an email with impacted... (7 Replies)
Discussion started by: vinil
7 Replies

8. UNIX for Beginners Questions & Answers

Code for checking if certain no of files exists

Hi, I am writing the shell script in ksh to check certain no of files exists,In my case there are 7 files exist like below Sales1_timstamp.csv Sales2_timstamp.csv Sales3_timstamp.csv Sales4_timstamp.csv Sales5_timstamp.csv Sales7_timstamp.csv Sales7_timstamp.csv Once all the files... (4 Replies)
Discussion started by: SRPR
4 Replies
halockrun(1M)						  System Administration Commands					     halockrun(1M)

NAME
halockrun - run a child program while holding a file lock SYNOPSIS
/usr/cluster/bin/halockrun [-nsv] [-e exitcode] lockfilename prog [args] DESCRIPTION
The halockrun utility provides a convenient means to claim a file lock on a file and run a program while holding that lock. As this utility supports script locking, this utility is useful when programming in scripting languages such as the Bourne shell. See sh(1). halockrun opens the file lockfilename and claims an exclusive mode file lock on the entire file. See fcntl(2) fcntl(2)). Then it runs the program prog with arguments args as a child process and waits for the child process to exit. When the child exits, halockrun releases the lock, and exits with the same exit code with which the child exited. The overall effect is that the child prog is run as a critical section, and that this critical section is well-formed, in that no matter how the child terminates, the lock is released. If the file lockfilename cannot be opened or created, then halockrun prints an error message on stderr and exits with exit code 99. You can run this command in the global zone or in a non-global zone. The command affects only the global or non-global zone in which you issue the command. OPTIONS
The following options are supported: -e exitcode Normally, errors detected by halockrun exit with exit code 99. The -e option provides a means to change this special exit code to a different value. -n The lock should be requested in non-blocking mode: if the lock cannot be granted immediately, halockrun exits immediately, with exit code 1, without running prog. This behavior is not affected by the -e option. Without the -n option, the lock is requested in blocking mode, thus, the halockrun utility blocks waiting for the lock to become available. -s Claim the file lock in shared mode, rather than in exclusive mode. -v Verbose output, on stderr. EXIT STATUS
Errors detected by halockrun itself, such that the child process was never started, cause halockrun to exit with exit code 99. (This exit code value can be changed to a different value using the -e option. See OPTIONS. Otherwise, halockrun exits with the same exit code with which the child exited. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWscu | +-----------------------------+-----------------------------+ |Interface Stability |Evolving | +-----------------------------+-----------------------------+ SEE ALSO
fcntl(2), attributes(5) Sun Cluster 3.2 10 Apr 2006 halockrun(1M)
All times are GMT -4. The time now is 02:26 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy