I’ve seen this line in many shell scripts but I don’t understand the effect it has. Could someone explain please?
tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/test$$
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It creates a temporary file and puts the path to it in the $tempfile variable.
runs the tempfile command (
man tempfile) and discards any error messages. If it succeeds, it returns the name of the newly created temporary file. If it fails, it returns non-zero, in which case the next part of the command runs.For a command
this || that,thatonly runs ifthisfails, i.e. returns non-zero.$$is a variable in bash that expands to the process ID of the shell. (Compare the results ofpsandecho $$.) Sotempfile=/tmp/test$$will expand to something liketempfile=/tmp/test2278.Presumably, later in the script, something writes to $tempfile.