I am trying to do the following task:
write a shell script called changedir
which
takes a directory name, a command name and (optionally) some additional arguments.
The script will then change into the directory indicated, and
executes the command indicated with the arguments provided.
Here an example:
$ sh changedir /etc ls -al
This should change into the /etc
directory and run the command ls -al
.
So far I have:
#!/bin/sh
directory=$1; shift
command=$1; shift
args=$1; shift
cd $directory
$command
If I run the above like sh changedir /etc ls
it changes and lists the directory. But if I add arguments to the ls
it does not work. What do I need to do to correct it?