Looking to understand what this command $$ does in variable assignment?


 
Thread Tools Search this Thread
Operating Systems Solaris Looking to understand what this command $$ does in variable assignment?
# 1  
Old 12-13-2012
Looking to understand what this command $$ does in variable assignment?

Hi Folks,

I'm looking to figure something out in an existing script I'm trying to understand.

the command in question(on a Solaris Box using KSH) is: WORKDIR=/tmp/namereplaced.exec.$$.$RANDOM

Now, I know it's setting the $workdir environmental variable...
And I understand most of the command.

What I don't get is what the ".$$." is doing?

When I run the command and then echo $WORKDIR, I get:
/tmp/namereplaced.12954.15085

On repeatedly running the command and echoing the variable, the second number (15085) changes each time as it's randomly generated.

But the first number (12954 above) remains the same. I am assuming that number comes from the ".$$." segment of the command, but I do not understand how or why?

Can anyone explain this?

Thanks!

Marc

Last edited by joeyg; 12-13-2012 at 03:30 PM.. Reason: Better question title
# 2  
Old 12-13-2012
$$ is a special variable meaning "the process ID of the current shell". If you quit and logged back in, you'd get a new one. It's not impossible for the same process ID to be repeated eventually, but two processes will never have the same process ID at the same time.

Sometimes you see it used in naming temporary files. Instead of /tmp/filename, you'd do /tmp/$$-filename, so that if someone manages to run two instances of your shell script simultaneously, they won't stomp over each other's tempfiles. Often you see mktemp used instead, which just generates a long, unique filename with no relation to your process id, since that makes it harder for outsiders to guess what process is using what file.

$$.$RANDOM Seems redundant to me, I'd have used one or the other, but I guess they were feeling paranoid that day. Or had requirements I'm not aware of yet.

Last edited by Corona688; 12-13-2012 at 03:29 PM..
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 12-13-2012
Thank you very much for the explanation!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Variable Assignment

Hi I am facing a problem. export local_folder=/opt/app/ cd /opt/app/abc/ abcversion="abc*" (abcga5 is inside /opt/app/abc/) echo $abcversion (it echoes the correct version as abcga5 ) Now when I reuse the value of abcversion for a below path: export... (6 Replies)
Discussion started by: ankur328
6 Replies

2. Shell Programming and Scripting

Same Variable Assignment

Hi I have a strange problem: In my shell script I am performing a copy task: . prop.txt cp -r $dir/ $dir/archive $dir is fetched from a property file (prop.txt) which stores its value dir=/opt/data Now the problem is another dir1 comes into picture. I only want to add... (1 Reply)
Discussion started by: ankur328
1 Replies

3. Shell Programming and Scripting

Trouble with variable and command assignment

I have a section of a script where I want to check a log file for a certain oracle error and if there is only one error (and it is ORA-39152) then I want to email that script is complete. Otherwise email failure and log. Somehow with this while the log only has one error and it is ORA-39152, I... (5 Replies)
Discussion started by: cougartrace
5 Replies

4. Programming

Need to understand the overloaded assignment operator behavior

Hi, In the following code, class A { public: void operator=(const A& rhs) { if (this == &rhs) cout << "self-assigned"; } }; class B { A a; // should not be a pointer member, (i.e) A* a }; int main() { B b; b = b; // Ans: self-assigned } I am really... (5 Replies)
Discussion started by: royalibrahim
5 Replies

5. Shell Programming and Scripting

Help with variable assignment

Hi, In AIX I have a variable with , (coma) separated values assigned to it like shown below var1=apple,boy,chris i want to convert this to var1='apple','boy','chris' the number of values assigned to var1 might change and it could be from 1 to n any suggestions please? (3 Replies)
Discussion started by: rahul9909
3 Replies

6. Shell Programming and Scripting

Dynamic variable assignment

Hi all, I’m very new to UNIX programming. I have a question on dynamic variable 1. I’m having delimited file (only one row). First of all, I want to count number of columns based on delimiter. Then I want to create number of variables equal to number of fields. Say number of... (5 Replies)
Discussion started by: mkarthykeyan
5 Replies

7. Shell Programming and Scripting

assignment to variable from eval command

Hi Gurus, I am having 2 parameters as below parm1=value1 parm2=parm1 I want to evaluate parm1 value using eval echo \$$parm2 and later i want to assign this value to other variable which i will be using in if statement like : if ]; then do this....... fi could you please suggest... (5 Replies)
Discussion started by: k_vikash
5 Replies

8. Shell Programming and Scripting

eval and variable assignment

Hi, i have an issue with eval and variable assignment. 1) i have a date value in a variable and that date is part of a filename, var1=20100331 file1=${var1}-D1-0092.xml.zip file2=${var2}-D2-0092.xml.zip file3=${var3}-D3-0092.xml.zip i am passing the above variables to a script via... (11 Replies)
Discussion started by: mohanpadamata
11 Replies

9. Shell Programming and Scripting

command output to variable assignment and screen simultaneously

Hello Folks, I have a script that runs a command (rsync) that sometimes takes a long time to complete and produces diagnostic output on stdout as it runs. I am currently capturing this output in a variable and using it further in the script. I would like to continue to capture this output... (2 Replies)
Discussion started by: mbm
2 Replies

10. UNIX for Dummies Questions & Answers

@ in a variable assignment

Hello Everybody, Does anyone know what the @ symbol means in a csh script, if used with a variable assignment as below @ line = 1 why not just use.... set line=1 Many thanks rkap (1 Reply)
Discussion started by: rkap
1 Replies
Login or Register to Ask a Question