Busybox sh wrapper to add additional functionality

I need a simple busybox sh utility that will do:

IF "-Q" PARAMETER IS PROVIDED THEN  
    acommand ALL PARAMETERS BUT "-Q" 2>&1 1>/dev/null  
ELSE  
    acommand ALL PARAMETERS  
FI

      

Parameters can include spaces.

BTW I want to run a script using busybox sh and doesn't support arrays.

+2


source to share


3 answers


You can do all this in the busybox

ash

shell:



#!/bin/sh
for i in "${@}"
do
    if [ "$i" = "-Q" ]
    then
        flagQ=1
    else
        args="$args \"$i\""
    fi
done
if [ "$flagQ" = "1" ]
then
    eval acommand "$args" 2>&1 1>/dev/null
else
    eval acommand "$args"
fi

      

+2


source


It's a shame you have to handle spaces in the arguments, otherwise this might work:

#!/bin/sh
Q=0
ARGS=

while [ $# -ge 1 ]; do
    case $1 in
        -Q)
            Q=1
            ;;
        *)
            ARGS="$ARGS $1"
            ;;
    esac
    shift
done

if [ $Q -eq 1 ] ; then
    acommand $ARGS 2>&1 1>/dev/null
else
    acommand $ARGS
fi

      

EDIT:



Thus, this version handles spaces by interpreting back ticks.

#!/bin/busybox ash
Q=0
ARGS=

while [ $# -ge 1 ]; do
    case $1 in
        -Q)
            Q=1
            ;;
        *)
            ARGS="$ARGS \"$1\""
            ;;
    esac
    shift
done

if [ "$Q" -eq 1 ] ; then
    eval acommand $ARGS 2>&1 1>/dev/null
else
    eval acommand $ARGS
fi

      

I think you have a complete solution that you will need to code in C, which will be a little ugly.

0


source


This uses bash arrays - but I can see from the comments on another answer that the code shouldn't work under bash (despite the bash tag originally applied to the question); it should work under busybox shell .

I'm pretty sure it doesn't answer the question, because the question is fundamentally irrefutable given busybox's constraints. In the past, I have used a special program I called "escape" to create an argument string that can be retrieved to get the original arguments β€” spaces and that's it. But this requires support from outside the shell.


This solution only uses bash. I'm not sure if this is completely idiomatic bash code, but it works.

#!/bin/bash

i=0
Qflag=0
for arg in "$@"
do
    if [ "X$arg" = "X-Q" ]
    then Qflag=1
    else args[$((i++))]=$arg
    fi
done

if [ $Qflag = 1 ]
then exec acommand "${args[@]}" 2>&1 >/dev/null
else exec acommand "${args[@]}"
fi

      

The first loops build the args array with the script arguments, except that it doesn't add '-Q' to the list and records its presence in the Qflag variable.

The if statement at the end indicates whether Qflag was set to 1, and if so, sends errors from "acommand" to stdout and sends stdout to / dev / null (which differs from the effect if I / O redirection is canceled - this will send standard output to / dev / null and send standard error to the same location, silencing "acommand").

Using "exec" is a trivial optimization that makes it easier to handle the exit status in this case.

Tested with "acommand" which prints its arguments on separate lines:

#!/bin/sh
for arg in "$@"
do echo "$arg"
done

      

and with commands like:

bash wrapper.sh -c -d 'arg with spaces'

      

which produces the output:

-c
-d
arg with spaces

      

Obviously there is no way out when redirecting I / O in place:

bash wrapper.sh -c -Q -d 'arg with spaces'

      

However, if you omit I / O redirection, you can see the same result.

0


source







All Articles