^$$ meaning

Hi ,

Can anyone please let me know whta the follwoing piece of code for

ScriptName=${0##*/}
if pgrep -f "$ScriptName" | grep -v "^$$\$" ; then
  echo `date`": Sctipt $ScritName is already runnig"
  exit
fi

Thnx a lot in advance

'man ksh' yeilds:

     $$     The process number of this shell.

Plus grep regex metacharacter ^ is beginning of line; in "..." the shell expands $$ first, else '^$$' is a line with just a '$', as in regex the second '$' makes the first literal, and the second '$' still means end of line.

Plus grep regex metacharacter \$ is end of line, so this is trying to match a line that contiains the shell PID only and displays everything else (-v option)

And the \ on the final $ is paranoia, as " . . . $" cannot be shell expanded, as there is no variable after $. I like to use the " less and the ' more, so the shell works less and there are fewer surprises: '^'"$$"'$'

Real paranoia indeed, the shell won't bite :wink:

grep -v ^$$$

Not for this, but familiarity breeds contempt and surprises. With '...' there are few surprises! We should teach best practices as we help with problems.

if pgrep -f "$S" | grep -v "^$$\$" ; then ...

That piece of code could be written:

if pidof -x -o $$ $criptName; then ...

which does the same job without further external command.
see Man Page for pidof (All Section 8) - The UNIX and Linux Forums

Backing up to look at the whole problem, I like to do already running with something like this:

#!/usr/bin/ksh
 
zp=$(fuser $log 2>/dev/null)
 
if [ "$zp" != "" -a $( ps -fp "$zp"|grep -c "${0##*/}" ) != 0 ]
then
 date "+%Y-%m-%d %H:%M:%S $$ ${0##*/} already running." >>$log
 exit
fi
 
{
 date "+%Y-%m-%d %H:%M:%S $$ ${0##*/} Start."
 .
 .
 .
 date "+%Y-%m-%d %H:%M:%S $$ ${0##*/} Finish."
} >>$log 2>&1