Skip to main content
replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/
Source Link

I am trying to implement error handling in my shell script as described in the 2nd answer in Best practice to use $? in bash?Best practice to use $? in bash?

My script looks like this:

#!/bin/bash try() { "$@" code=$? if [ $code -ne 0 ] then echo "oops $1 didn't work" exit 1 fi } try myvar=$(mktemp -p ./) 

The script exits with a

./test.sh: line 4: myvar=./tmp.scNLzO1DDi: No such file or directory oops myvar=./tmp.scNLzO1DDi didn't work 

Just,

myvar=$(mktemp -p ./) 

of course works fine, and $myvar returns the full path and name of the temp file.

How can I get the statement to assign the name of the tmp file to the variable myvar, while still passing the entire statement and it's results to try() so try() can do what it needs to? Thanks.

I am trying to implement error handling in my shell script as described in the 2nd answer in Best practice to use $? in bash?

My script looks like this:

#!/bin/bash try() { "$@" code=$? if [ $code -ne 0 ] then echo "oops $1 didn't work" exit 1 fi } try myvar=$(mktemp -p ./) 

The script exits with a

./test.sh: line 4: myvar=./tmp.scNLzO1DDi: No such file or directory oops myvar=./tmp.scNLzO1DDi didn't work 

Just,

myvar=$(mktemp -p ./) 

of course works fine, and $myvar returns the full path and name of the temp file.

How can I get the statement to assign the name of the tmp file to the variable myvar, while still passing the entire statement and it's results to try() so try() can do what it needs to? Thanks.

I am trying to implement error handling in my shell script as described in the 2nd answer in Best practice to use $? in bash?

My script looks like this:

#!/bin/bash try() { "$@" code=$? if [ $code -ne 0 ] then echo "oops $1 didn't work" exit 1 fi } try myvar=$(mktemp -p ./) 

The script exits with a

./test.sh: line 4: myvar=./tmp.scNLzO1DDi: No such file or directory oops myvar=./tmp.scNLzO1DDi didn't work 

Just,

myvar=$(mktemp -p ./) 

of course works fine, and $myvar returns the full path and name of the temp file.

How can I get the statement to assign the name of the tmp file to the variable myvar, while still passing the entire statement and it's results to try() so try() can do what it needs to? Thanks.

Tweeted twitter.com/#!/StackUnix/status/509306488209559552

Shell script error handling while assigning STOUTSTDOUT to variable

Source Link
Jim Walker
  • 185
  • 1
  • 7

Shell script error handling while assigning STOUT to variable

I am trying to implement error handling in my shell script as described in the 2nd answer in Best practice to use $? in bash?

My script looks like this:

#!/bin/bash try() { "$@" code=$? if [ $code -ne 0 ] then echo "oops $1 didn't work" exit 1 fi } try myvar=$(mktemp -p ./) 

The script exits with a

./test.sh: line 4: myvar=./tmp.scNLzO1DDi: No such file or directory oops myvar=./tmp.scNLzO1DDi didn't work 

Just,

myvar=$(mktemp -p ./) 

of course works fine, and $myvar returns the full path and name of the temp file.

How can I get the statement to assign the name of the tmp file to the variable myvar, while still passing the entire statement and it's results to try() so try() can do what it needs to? Thanks.