Re: bash (sh?) scripting question
- From: "Chris F.A. Johnson" <cfajohnson@xxxxxxxxx>
- Date: Wed, 6 Sep 2006 06:30:50 -0400
On 2006-09-06, Leonardo wrote:
Hi,
I am trying to implement a type of "try" function, that will execute a
command and verify the exit code:
try()
{
eval $*
To maintain the argumnts are they are received, use "$@":
eval "$@"
if [ $? -ne 0 ]; then
echo "failed"
exit $?
At this point, $? contains the result of the echo command.
fi
}
I wish to send it a command like this:
SOMEFILE="/some path/with spaces"
mv "$SOMEFILE" "$SOMEFILE.backup"
But the quotes are causing a problem:
try "mv \"$SOMEFILE\" \"$SOMEFILE.bak\""
try mv "$SOMEFILE" "$SOMEFILE.backup"
Both of these last 2 lines fail.
I used this to see how parameters were passed:
try
{
for param in $*
for param in "$@"
do
echo "Param: $param"
done
}
it showed that in the last case, despite putting quotes around the variable
name, it ends up being split everwhere there are spaces.
This is weird and I don't know how to solve it.
try()
{
eval "$@"
_TRY=$?
if [ $_TRY -ne 0 ]; then
echo "failed"
exit $_TRY
fi
}
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
.
- Follow-Ups:
- Re: bash (sh?) scripting question
- From: Kaz Kylheku
- Re: bash (sh?) scripting question
- References:
- bash (sh?) scripting question
- From: Leonardo
- bash (sh?) scripting question
- Prev by Date: About the batch command
- Next by Date: best way to have a script read a config file?
- Previous by thread: Re: bash (sh?) scripting question
- Next by thread: Re: bash (sh?) scripting question
- Index(es):
Relevant Pages
|
|