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
while(n)						       Tcl Built-In Commands							  while(n)

__________________________________________________________________________________________________________________________________________________

NAME
while - Execute script repeatedly as long as a condition is met SYNOPSIS
while test body _________________________________________________________________ DESCRIPTION
The while command evaluates test as an expression (in the same way that expr evaluates its argument). The value of the expression must a proper boolean value; if it is a true value then body is executed by passing it to the Tcl interpreter. Once body has been executed then test is evaluated again, and the process repeats until eventually test evaluates to a false boolean value. Continue commands may be exe- cuted inside body to terminate the current iteration of the loop, and break commands may be executed inside body to cause immediate termi- nation of the while command. The while command always returns an empty string. Note: test should almost always be enclosed in braces. If not, variable substitutions will be made before the while command starts execut- ing, which means that variable changes made by the loop body will not be considered in the expression. This is likely to result in an infinite loop. If test is enclosed in braces, variable substitutions are delayed until the expression is evaluated (before each loop iter- ation), so changes in the variables will be visible. For an example, try the following script with and without the braces around $x<10: set x 0 while {$x<10} { puts "x is $x" incr x } SEE ALSO
break(n), continue(n), for(n), foreach(n) KEYWORDS
boolean value, loop, test, while Tcl while(n)
All times are GMT -4. The time now is 08:15 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy