Prepare command before executing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Prepare command before executing
# 1  
Old 11-16-2010
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
Code:
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:
Code:
ls()
{
Param1=$1
exec $(ls "$Param1")
}

any help? Platform is linux ..


Thanks in advance..
# 2  
Old 11-16-2010
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 ...
Code:
for i in /etc /usr/var/cron
do
ls -lrt $i
done

or put your list in a file and scan it
Code:
[root@lyisy22:~/sand]# cat list
file1
file2
file3
file4
[root@lyisy22:~/sand]# while read f
> do
> ls -l $f
> done<list

# 3  
Old 11-16-2010
You need to load the function before using it, eg:
Code:
. myLsFunction

You also need to fix its code that way:
Code:
exec $(ls "$Param1")

should be
Code:
/bin/ls $param1

However, an alias seems more adequate for that particular need.
# 4  
Old 11-16-2010
Im trying to modify the "command" parameters itself

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...
# 5  
Old 11-16-2010
Quote:
Originally Posted by ctsgnb
Code:
[root@lyisy22:~/sand]# cat list
file1
file2
file3
file4
[root@lyisy22:~/sand]# while read f
> do
> ls -l $f
> done<list

You neither need a loop here, simply run:
Code:
ls -l $(<list)

# 6  
Old 11-16-2010
Hi jlliagre,

it didnt work for my command..
Ill paste the original code here ..just to be sure..file tmp.ksh
Code:
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..
# 7  
Old 11-16-2010
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 \" ?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Warn Before Executing Particular Command

I'm running CentOS 6.8 and use bash. I would like a warning to appear to the user who runs the command "service httpd restart" E.g. # service httpd restart are you sure y/n n # (or if y, the command executes). I looked into it a little but am not sure of the best approach. Aliases I ... (1 Reply)
Discussion started by: spacegoose
1 Replies

2. Shell Programming and Scripting

Executing a command in subshell

Hi All, I am trying to create a shell script which runs on my linux machine. The requirement is so that, ade createview xyz -> this steps creates a view ade useview xyz -> we are entering inside the view ade begintrans -> begin a transaction My script has following code : lets say... (2 Replies)
Discussion started by: Dish
2 Replies

3. Shell Programming and Scripting

Help executing command with options

Hi, I have this command in a shell script and I can get it to echo ok, but when I try to execute the command I get a "file not found" error. Which is strange because, if I copy and paste the same command at the cli it works ok. What am I doing wrong please? (16 Replies)
Discussion started by: bbbngowc
16 Replies

4. Solaris

Role not executing command

Hello Guys, I am studying RBAC. So I create a role called sysadm and gave it the "shutdown" profile. Now when I switch to that role, and execute the shutdown command $ shutdown -y -g0 -i5 The system responds with : shutdown: not found Can anyone help me with this please?... (1 Reply)
Discussion started by: cjashu
1 Replies

5. UNIX for Dummies Questions & Answers

executing a command using ssh

Hi All, I am trying to execute a command using ssh as below. ssh user123@servername "which ctmcontb" It is gving the error as below no ctmcontb in /usr/bin /usr/sbin /opt/sysadm/bin Not sure from where the PATH is getting picked up. But When I login direclty to the server I am... (5 Replies)
Discussion started by: anilreddy103
5 Replies

6. Shell Programming and Scripting

cd command not executing in if else loop

Hi, I am executing below script s1=`pwd` s2=/space if then echo "done" else echo "mistake" cd /tmp fi I am not able to migrate to /tmp directory if the condition is not true.However mistake is being printed.Means cd command is not working here.All other commands except cd are... (3 Replies)
Discussion started by: d8011
3 Replies

7. UNIX for Dummies Questions & Answers

Doing a capture while another command is executing?

Basically what i'm trying to do is execute an update command and at the same time have the system do a TCPdump to file for that update traffic. So I would like to connect the two commands so that the tcpdump terminates automatically when the update finishes/fails/whatever. Right now I have... (0 Replies)
Discussion started by: MrEddy
0 Replies

8. Shell Programming and Scripting

executing command from subdirectories

Hello, I've been trying to run 'ls -1R | wc -l' inside of sub directories to in order to determine how big each folder is. find . -maxdepth 1 -type d | while read folder do cd "$folder" && echo "$folder has $(ls -1R | wc -l) files" && cd .. done or for... (3 Replies)
Discussion started by: NoobProgrammer
3 Replies

9. Shell Programming and Scripting

Executing a command at startup

Hey all, How do I execute a file at startup automatically. From what I've read is that I need to put it into my .bashrc file. I'm not sure where to go from there. Can I just type commands into that and they'll run next time I restart my server? Right now I have added these lines: cd... (2 Replies)
Discussion started by: NathanWarden
2 Replies

10. UNIX for Dummies Questions & Answers

Automatically executing a command

Hi I was wondering if anyone knew the answer to this question? I am trying to find a way of executing a command if a certain file is created in the same directory. One way I thought about doing this was to create a FORTRAN program that continually searches for this file. If the file... (8 Replies)
Discussion started by: robbiegregg
8 Replies
Login or Register to Ask a Question