Command Substitution Question
Command Substitution Question
am 14.09.2007 21:12:49 von horvat.johann
Hi,
What I want to do with bash is:
If $2 is "silent" set OUT_DEV to "2>&- >&2":
if [ "$2" = "silent" ]
then
OUT_DEV="2>&- >&2"
else
OUT_DEV=""
fi
later in my code I wat to use it like this
rm filename $OUT_DEV
which should be substituted like this:
rm filename 2>&- >&2, so stdout and stderr are closed, if silent was
given as the second argument, but this doesn't work, the above string
will be substituded like this:
rm filename '2>&-' '>&2'
What am I doing wrong?
Thanks and best regards
Johann
Re: Command Substitution Question
am 14.09.2007 21:26:15 von Stephane CHAZELAS
2007-09-14, 12:12(-07), horvat.johann@gmx.net:
> Hi,
>
> What I want to do with bash is:
> If $2 is "silent" set OUT_DEV to "2>&- >&2":
>
> if [ "$2" = "silent" ]
> then
> OUT_DEV="2>&- >&2"
> else
> OUT_DEV=""
> fi
>
> later in my code I wat to use it like this
> rm filename $OUT_DEV
> which should be substituted like this:
> rm filename 2>&- >&2, so stdout and stderr are closed, if silent was
> given as the second argument, but this doesn't work, the above string
> will be substituded like this:
> rm filename '2>&-' '>&2'
> What am I doing wrong?
[...]
Do you really expect
STRING="foo > bar"
echo $STRING
to write "foo" into the "bar" file?
It's very wrong and dangerous to close any of the descriptors 0,
1 or 2 as commands don't expect those to be closed and can
behave very strangely if they are. 2>&- >&2 is also incorrect as
you're trying to dupplicate a closed fd.
Try:
[ "$2" = silent ] && exec > /dev/null 2>&1
--
Stéphane
Re: Command Substitution Question
am 14.09.2007 22:11:06 von horvat.johann
Dear St=E9phane,
Okay you are wright:
Closing the descriptors wasn't a good idea, and redirecting to /dev/
null is much better, but
I have a lot of lines in my code where I would have to hardcode
> /dev/null 2>&1
which I want to avoid.
So I would like to define soemthing like the OUTPUT-REDIRECTION-STRING
wich should be emty, if no redirection is desired and which holds the
"> /dev/null 2>&1" if no output is desired.
How can I achive that?
Thanks
Johann
Re: Command Substitution Question
am 14.09.2007 22:11:54 von Miles
On Sep 14, 2:12 pm, horvat.joh...@gmx.net wrote:
> Hi,
>
> What I want to do with bash is:
> If $2 is "silent" set OUT_DEV to "2>&- >&2":
>
> if [ "$2" = "silent" ]
> then
> OUT_DEV="2>&- >&2"
> else
> OUT_DEV=""
> fi
>
> later in my code I wat to use it like this
> rm filename $OUT_DEV
> which should be substituted like this:
> rm filename 2>&- >&2, so stdout and stderr are closed, if silent was
> given as the second argument, but this doesn't work, the above string
> will be substituded like this:
> rm filename '2>&-' '>&2'
> What am I doing wrong?
>
> Thanks and best regards
> Johann
Why not write a function, execute_command. It will take in the command
to be run. In there you can use $OUT_DEV. It might work something like
this:
execute_command () {
STEP=$1
COMMAND=$2
COMMAND_FILE=$WORK_DIR/$$.tmp_command_file
print "#!/usr/bin/
ksh" >
$COMMAND_FILE
print "$COMMAND $OUT_DEV" >>
$COMMAND_FILE # ************ NOTE this line
*********************
print "exit \
$?" >>
$COMMAND_FILE
#(( $DEBUG == $TRUE )) && cat $COMMAND_FILE
chmod +x $COMMAND_FILE
print "Step: $STEP...\c"
$COMMAND_FILE
RETCODE1=$?; if (( $RETCODE == 0 )); then RETCODE=$RETCODE1; fi
rm $COMMAND_FILE
RETCODE1=$?; if (( $RETCODE == 0 )); then RETCODE=$RETCODE1; fi
if (( $RETCODE1 != 0 ))
then
print "ERROR: Error $STEP."
error_exit
else
print "ok.\n"
fi
}
Note the line that prints $COMMAND $OUT_DEV. IF $OUT_DEV is blank,
nothing happens. Or, set $OUT_DEV to ">/dev/null 2>&1" (as previsiouly
noted).
The function is called like:
STEP="changing permissions of mount point"
COMMAND="chmod 777 $MOUNT_POINT"
execute_command "$STEP" "$COMMAND"
Miles
Re: Command Substitution Question
am 14.09.2007 22:15:16 von Miles
On Sep 14, 3:11 pm, Miles wrote:
> On Sep 14, 2:12 pm, horvat.joh...@gmx.net wrote:
>
>
>
> > Hi,
>
> > What I want to do with bash is:
> > If $2 is "silent" set OUT_DEV to "2>&- >&2":
>
> > if [ "$2" = "silent" ]
> > then
> > OUT_DEV="2>&- >&2"
> > else
> > OUT_DEV=""
> > fi
>
> > later in my code I wat to use it like this
> > rm filename $OUT_DEV
> > which should be substituted like this:
> > rm filename 2>&- >&2, so stdout and stderr are closed, if silent was
> > given as the second argument, but this doesn't work, the above string
> > will be substituded like this:
> > rm filename '2>&-' '>&2'
> > What am I doing wrong?
>
> > Thanks and best regards
> > Johann
>
> Why not write a function, execute_command. It will take in the command
> to be run. In there you can use $OUT_DEV. It might work something like
> this:
>
> execute_command () {
>
> STEP=$1
> COMMAND=$2
>
> COMMAND_FILE=$WORK_DIR/$$.tmp_command_file
> print "#!/usr/bin/
> ksh" >
> $COMMAND_FILE
> print "$COMMAND $OUT_DEV" >>
> $COMMAND_FILE # ************ NOTE this line
> *********************
> print "exit \
> $?" >>
> $COMMAND_FILE
>
> #(( $DEBUG == $TRUE )) && cat $COMMAND_FILE
> chmod +x $COMMAND_FILE
> print "Step: $STEP...\c"
>
> $COMMAND_FILE
> RETCODE1=$?; if (( $RETCODE == 0 )); then RETCODE=$RETCODE1; fi
> rm $COMMAND_FILE
> RETCODE1=$?; if (( $RETCODE == 0 )); then RETCODE=$RETCODE1; fi
> if (( $RETCODE1 != 0 ))
> then
> print "ERROR: Error $STEP."
> error_exit
> else
> print "ok.\n"
> fi
>
> }
>
> Note the line that prints $COMMAND $OUT_DEV. IF $OUT_DEV is blank,
> nothing happens. Or, set $OUT_DEV to ">/dev/null 2>&1" (as previsiouly
> noted).
>
> The function is called like:
> STEP="changing permissions of mount point"
> COMMAND="chmod 777 $MOUNT_POINT"
> execute_command "$STEP" "$COMMAND"
>
> Miles
Too bad that didn't paste so well...
execute_command () {
STEP=$1
COMMAND=$2
COMMAND_FILE=$WORK_DIR/$$.tmp_command_file
print "#!/usr/bin/ksh" > $COMMAND_FILE
print "$COMMAND $OUT_DEV" >>$COMMAND_FILE #Note here
print "exit \$?" >>$COMMAND_FILE
#(( $DEBUG == $TRUE )) && cat $COMMAND_FILE
chmod +x $COMMAND_FILE
print "Step: $STEP...\c"
$COMMAND_FILE
RETCODE1=$?; if (( $RETCODE == 0 )); then RETCODE=$RETCODE1; fi
rm $COMMAND_FILE
RETCODE1=$?; if (( $RETCODE == 0 )); then RETCODE=$RETCODE1; fi
if (( $RETCODE1 != 0 ))
then
print "ERROR: Error $STEP."
error_exit
else
print "ok.\n"
fi
}
Re: Command Substitution Question
am 14.09.2007 22:18:04 von Miles
On Sep 14, 3:11 pm, horvat.joh...@gmx.net wrote:
> Dear St=E9phane,
>
> Okay you are wright:
> Closing the descriptors wasn't a good idea, and redirecting to /dev/
> null is much better, but
> I have a lot of lines in my code where I would have to hardcode> /dev/nul=
l 2>&1
>
> which I want to avoid.
> So I would like to define soemthing like the OUTPUT-REDIRECTION-STRING
> wich should be emty, if no redirection is desired and which holds the
> "> /dev/null 2>&1" if no output is desired.
>
> How can I achive that?
>
> Thanks
> Johann
OUT_DEV=3D""
STEP=3D"changing permissions of mount point"
COMMAND=3D"chmod 777 $MOUNT_POINT"
execute_command "$STEP" "$COMMAND"
OUT_DEV=3D">/dev/null 2>&1"
STEP=3D"changing ownership of mount point"
COMMAND=3D"chown bin:staff $MOUNT_POINT"
execute_command "$STEP" "$COMMAND"
etc...
Re: Command Substitution Question
am 15.09.2007 10:41:16 von Stephane CHAZELAS
2007-09-14, 13:11(-07), horvat.johann@gmx.net:
> Dear Stéphane,
>
> Okay you are wright:
> Closing the descriptors wasn't a good idea, and redirecting to /dev/
> null is much better, but
> I have a lot of lines in my code where I would have to hardcode
>> /dev/null 2>&1
> which I want to avoid.
> So I would like to define soemthing like the OUTPUT-REDIRECTION-STRING
> wich should be emty, if no redirection is desired and which holds the
> "> /dev/null 2>&1" if no output is desired.
[...]
exec > /dev/null 2>&1
redirects every output from now on.
Or, if you don't want to redirect every command, you can do:
if quiet; then
exec 3> /dev/null 4>&3
else
exec 3>&1 4>&2
fi
cmd1
cmd2 >&3 2>&4
--
Stéphane