User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Shell Scripting section within the Software Development category of DaniWeb, a massive community of 423,514 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,618 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Shell Scripting advertiser: Programming Forums
Views: 626 | Replies: 2 | Solved
Reply
Join Date: Nov 2007
Posts: 118
Reputation: k2k is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
k2k k2k is offline Offline
Junior Poster

101 scripting questions

  #1  
Jun 25th, 2008
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2008
Posts: 39
Reputation: omrsafetyo is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 8
omrsafetyo omrsafetyo is offline Offline
Light Poster

Re: 101 scripting questions

  #2  
Jun 27th, 2008
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:
[code]
PATH=${PATH}PWD
[code]
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.
Last edited by omrsafetyo : Jun 27th, 2008 at 10:56 pm.
Reply With Quote  
Join Date: Nov 2007
Posts: 118
Reputation: k2k is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
k2k k2k is offline Offline
Junior Poster

Re: 101 scripting questions

  #3  
Jun 28th, 2008
Originally Posted by omrsafetyo View Post
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:
[code]
PATH=${PATH}PWD
[code]
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Shell Scripting Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the Shell Scripting Forum

All times are GMT -4. The time now is 4:43 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC