# How to use shell script?



## chhorvorn (Aug 21, 2008)

my shell script is always refuse with me when I try to write it.
I don't know how to use shell script could you guid me some point.
I am very glad to see your reply.
thans


----------



## shuuhen (Sep 4, 2004)

What shell is it written in? What's the exact error you get when trying to run it? Make sure it has the executable permission set for the user or group trying to run it.

There's a couple of guides to bash shell scripting on tldp.org. Check under Guides. There should be both an Advanced Bash Scripting and a Bash for Beginners.


----------



## lensman3 (Oct 19, 2007)

Shell scripts need execute permission to run. Add "chmod +x <script.sh>" where <script.sh> is you script name. If the script is in the same directory you are trying to execute it from, you must prepend the script name with "./" or "./script.sh". The reason for this is that the current working directory (CWD) is not in the PATH as it is automatically for DOS. It is considered a security hole to have the CWD in the PATH. To see what your path is type :"echo $PATH". If you do CWD add it at the end and make it a . (dot). 

You can execute a script without setting the execute bits by typing: "sh ./script.sh". This will execute it script. For debugging run it as "sh -x ./script.sh" or "sh -v ./script.sh" or "sh -vx ./script.sh". When the script is debugged move it to your ~/bin directory and add the execute bits.

The first line of the script is usually the engine that is going to run the script. If it the sh shell it is usually "#!/bin/sh". The #! IDs the file to be executed by the sh shell. Likewise for bash, csh, perl etc.

Go looking through /usr/bin at the various scripts that are there.

Hope this helps.


----------

