first of all, what can i do to make my command works like the "ls, cp, mv" ?
I always do vi myScript.sh and then chmod +x myScript.sh and I have to execute it like ./myScript.sh ,,, simply typing $myScript at the command promp will not work.


Second question for shell scripting:
I am trying to search whatever the argument is within only the user path... did I do this correctly? Thanks

PATH=/bin:/usr/bin/:sbin/. 
for command in PATH
do
     grep "$1"
done

my ideal output would be
> search ls
>/bin/ls something like that

Recommended Answers

All 2 Replies

In order just to type the name of the file to execute, it needs to be in an executable directory - so for e.g. if you have a script that is used frequently, drop it in /usr/local/bin and it will become executable from anywhere. You can also add the current directory as an executable path with:

PATH=${PATH}:$PWD

However, please note that this can be extremely dangerous - because then any script in the current directory can accidentally be run.

I'm not quite sure what your second question is getting at. It sounds like you are trying to recreate the "which" command.

If you type which ls it will tell you which ls your local environment executes.

$ which ls
/bin/ls

In order to recreate this, you would have to delimit the user's path variable:

command=$1
for path in `echo $PATH | sed "s/:/ /g"`
do
   ls $path | grep $command
done

Unlike which, this will return all installed instances (in your path) - the which command will only return the first instance found, which is the program that would actually be executed on your system.

In order just to type the name of the file to execute, it needs to be in an executable directory - so for e.g. if you have a script that is used frequently, drop it in /usr/local/bin and it will become executable from anywhere. You can also add the current directory as an executable path with:

PATH=${PATH}:$PWD

However, please note that this can be extremely dangerous - because then any script in the current directory can accidentally be run.

I'm not quite sure what your second question is getting at. It sounds like you are trying to recreate the "which" command.

If you type which ls it will tell you which ls your local environment executes.

$ which ls
/bin/ls

In order to recreate this, you would have to delimit the user's path variable:

command=$1
for path in `echo $PATH | sed "s/:/ /g"`
do
   ls $path | grep $command
done

Unlike which, this will return all installed instances (in your path) - the which command will only return the first instance found, which is the program that would actually be executed on your system.

thanks alot... yes, i was writing my own version of the which. I have one more question and i think i can get it done. I'll post the code in another thread. Thanks for the help again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.