Prepare command before executing

Hi,

Couldnt find the right string" to search for a similar question..so dont know if this has been answered yet...problem is that I want to prepare a command with the requisite parameters passed as a string before executing it...eg: the ls command ..

I can pass "-l", "-t" as parameters and call the generic function as

ls "-l"    ## here ls would be the function called and not the generic ls command.
ls "-t" 

etc..

the function I wrote was comething like:

ls()
{
Param1=$1
exec $(ls "$Param1")
}

any help? Platform is linux ..

Thanks in advance..

you don't need to write a function just to perform an ls command : just ls -lt *
I think you problem is more about passing a list of file you want to ls so ...

for i in /etc /usr/var/cron
do
ls -lrt $i
done

or put your list in a file and scan it

[root@lyisy22:~/sand]# cat list
file1
file2
file3
file4
[root@lyisy22:~/sand]# while read f
> do
> ls -l $f
> done<list

You need to load the function before using it, eg:

. myLsFunction

You also need to fix its code that way:

exec $(ls "$Param1")

should be

/bin/ls $param1

However, an alias seems more adequate for that particular need.

Hi,

Thanks for the early reply..
the ls was just an example...Im trying to modify another command which I have used at about a 100 places in the script...so to make it generic I wanted to write a function..

the command uses parameter a,b,c which perform totally different functions..

The ls command parameters need to be modified instead of the "file" parameters being passed...

You neither need a loop here, simply run:

ls -l $(<list)

Hi jlliagre,

it didnt work for my command..
Ill paste the original code here ..just to be sure..file tmp.ksh

DialogBox ()  #Generic call to Dialog Function in Linux
{
Param1=$1               #IdSet
Param2=$2               #Title
Param3=$3               #Backtitle
Param4=$4               #Function
shift; shift; shift; shift;
ParamGeneric=$*         #Function Parameters

echo $Param1
echo $Param2
echo $Param3
echo $Param4
echo $ParamGeneric

dialog --title \"$Param2\" \
           --backtitle \"Param3\" \
           --$Param4 \"$ParamGeneric\" 10 70)


DialogBox 1 "Run"  "Run files on environment" msgbox  "  Warning...not tested on production ..for obvious reasons"


hope this clears it furthur..
the parameters being passed are expanded and thus acsue the number of arguments to grow...

running under ksh / Linux..

Please provide more clues about what doesn't work, with error messages.
There is an extra closing parenthesis, param3 is missing a leading $, the script ending is dubious/truncated.
Why are you escaping the double quotes like this \" ?

ok...

the dialog command can have parameters like msgbox,yesno,inputbox, etc.. here Im testing with msgbox

what doesn't work:

The parameters being passed expand and thus cause the final dialog command to have more than the required parameters.

error messages.:

Error: Unknown option Run".
Use --help to list options.

*There is an extra closing parenthesis ( corrected )

*param3 is missing a leading $ (corrected)

*the script ending is dubious/truncated (repasting).

*Why are you escaping the double quotes like this \" ? ( to prevent the parameters from expanding when they are passed )

DialogBox ()  #Generic call to Dialog Function in Linux
{
Param1=$1               #IdSet
Param2=$2               #Title
Param3=$3               #Backtitle
Param4=$4               #Function
shift; shift; shift; shift;
ParamGeneric=$*         #Function Parameters

echo $Param1
echo $Param2
echo $Param3
echo $Param4
echo $ParamGeneric
set -x
dialog --title \"$Param2\" \
           --backtitle \"$Param3\" \
           --$Param4 \"$ParamGeneric\" 10 70
}


DialogBox 1 "Autosys Run"  "Run Autosys Jil files on environment" msgbox  "  Warning...not tested on production ..for obvious reasons"

Doesn't make sense. They have already been expanded anyway.
Remove these bogus backslashes and your code should just work.

1 Like

oops..

thanks a ton sir..

:b:

Sure, that was just for him to have an example.

By the way, if there are numerous files, not sure the $(<file) notation would work ( arg list too long )

Correct. In such case

xargs ls -l <list

is still better than a loop.

Yep, as i said : i post it just as an example. So he is then free to adapt to his needs : add many command into the loop, break line into field stored in variables and so on... stuff that cannot be achieve easily with xargs .

But you are true that in this simple case, xargs is a better way (by the way, i never pretend the contrary) :wink: