my first shell script

Hey guys,

i am completely new to shell scripting. i have a problem which i am unable to resolve. here is what i did. i use cygwin on my computer

i opened vi editor and wrote my first shell script

#!/bin/bash
#SS1
#Usage:SS1
ls

i came back on console and ran this command :
$ SS1

I got an error bash : command not found.

This whole situation might sound really dumb , but please understand that i am completely new to cygwin.

thanks
Arsenalboy

As it's your first shell script...

try

./SS1

Your PATH probably does not contain the current directory. Type

echo $PATH

to see your full PATH

To execute your script try

./SSI

As it was the guy's first shell script, I didn't want to confuse him with paths and stuff.

But I don't know cygwin much, and I don't know if he needs to chmod the file first, otherwise we're looking at

chmod 755 SS1
./SS1

or

bash ./SS1

It can all get a bit much.

Also, setting the executable bit by running

chmod u+x SS1

might help

i opened vi editor and wrote my first shell script and saved it as SS1.sh

#!/bin/bash
#SS1
#Usage:SS1
ls

--came back onto console and tried the following
./SS1.sh
SS1.sh

Note: the shell script is stored on my D drive D:/scripts and the console on cygwin also points to the same folder D:/scripts

I tried echo $PATH and this is what i get
/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/cygdrive/d/Perl/site/bin:/cygdrive/d/Perl/bin:/cygdrive/c/sybasenew1/DataAccess/ODBC/dll:/cygdrive/c/sybasenew1/RPL-15_0/bin:/cygdrive/c/sybasenew1/Shared/Sybase Central 4.3:/cygdrive/c/sybasenew1/UAF-2_0/bin:/cygdrive/c/sybasenew1/OCS-15_0/lib3p:/cygdrive/c/sybasenew1/OCS-15_0/dll:/cygdrive/c/sybasenew1/OCS-15_0/bin:/cygdrive/c/sybasenew1/ASE-15_0/dll:/cygdrive/c/sybasenew1/ASE-15_0/bin:/cygdrive/d/sybasenew1/DataAccess/ODBC/dll:/cygdrive/d/sybasenew1/RPL-15_0/bin:/cygdrive/d/sybasenew1/Shared/Sybase Central 4.3:/cygdrive/d/sybasenew1/UAF-2_0/bin:/cygdrive/d/sybasenew1/OCS-15_0/lib3p:/cygdrive/d/sybasenew1/OCS-15_0/dll:/cygdrive/d/sybasenew1/OCS-15_0/bin:/cygdrive/d/sybasenew1/ASE-15_0/dll:/cygdrive/d/sybasenew1/ASE-15_0/bin:/cygdrive/d/sybasenew/OCS-15_0/lib3p:/cygdrive/d/sybasenew/OCS-15_0/dll:/cygdrive/d/sybasenew/OCS-15_0/bin:/cygdrive/d/sybasenew/ASE-15_0/dll:/cygdrive/d/sybasenew/ASE-15_0/bin:/cygdrive/c/DataAccess/ODBC/dll:/cygdrive/c/RPL-15_0/bin:/cygdrive/c/Shared/Sybase Central 4.3:/cygdrive/c/UAF-2_0/bin:/cygdrive/c/OCS-15_0/lib3p:/cygdrive/c/OCS-15_0/dll:/cygdrive/c/OCS-15_0/bin:/cygdrive/c/ASE-15_0/dll:/cygdrive/c/ASE-15_0/bin:/cygdrive/c/DataAccess/ODBC/dll:/cygdrive/c/RPL-15_0/bin:/cygdrive/c/Shared/Sybase Central 4.3:/cygdrive/c/UAF-2_0/bin:/cygdrive/c/OCS-15_0/lib3p:/cygdrive/c/OCS-15_0/dll:/cygdrive/c/OCS-15_0/bin:/cygdrive/c/ASE-15_0/dll:/cygdrive/c/ASE-15_0/bin:/cygdrive/c/sybase/DataAccess/ODBC/dll:/cygdrive/c/sybase/RPL-15_0/bin:/cygdrive/c/sybase/Shared/Sybase Central 4.3:/cygdrive/c/sybase/UAF-2_0/bin:/cygdrive/c/sybase/OCS-15_0/lib3p:/cygdrive/c/sybase/OCS-15_0/dll:/cygdrive/c/sybase/OCS-15_0/bin:/cygdrive/c/sybase/ASE-15_0/dll:/cygdrive/c/sybase/ASE-15_0/bin:/cygdrive/c/WINDOWS/system32:/cygdrive/c/WINDOWS:/cygdrive/c/WINDOWS/System32/Wbem:/cygdrive/c/sybase/DBISQL/bin:/cygdrive/c/DBISQL/bin:/cygdrive/c/DBISQL/bin:/cygdrive/d/sybasenew1/DBISQL/bin:/cygdrive/c/sybasenew1/DBISQL/bin:/cygdrive/c/Program Files/QuickTime/QTSystem/:/cygdrive/c/Program Files/Microsoft SQL Server/100/Tools/Binn/:/cygdrive/c/Program Files/Microsoft SQL Server/100/DTS/Binn/:/cygdrive/c/Program Files/Microsoft SQL Server/100/Tools/Binn/VSShell/Common7/IDE/:/cygdrive/c/Program Files/Microsoft Visual Studio 9.0/Common7/IDE/PrivateAssemblies/:/cygdrive/c/WINDOWS/system32/WindowsPowerShell/v1.0

please help
thanks
Arsenal boy

Well if the script is called SS1.sh, then you should do everything we suggested, but replacing SS1 with SS1.sh

To summarise:

chmod u+x SS1.sh
PATH=.:$PATH
SS1.sh

awesome, it was successful . i understand that chmod u+x is to make the ss1.sh executable. but what does PATH=.:$PATH do ?

See? That's why I didn't want to tell you!

Your current directory can be referenced as . (dot)

That's why ./SS1 refers to SS1 in your current directory.

To avoid using ./SS1 and use just SS1, you need to add your current directory (.) to your path. PATH=.:$PATH adds . (your current directory) to your path.

It is highly recommended that you don't put (.) in your PATH for security reasons. Either call the script with the path, or put the script in a directory that is already on your path, or add a specific directory to your path and put the script there.

I totally agree.

Cheers Guys! I learned a lot today.

Hi Irishboy, provide an 'execute' permission to the file..
chmod 766 <filename>
This will give -rwxrw-rw- permission to the file. Now try running your script.

I would recommend 744 or 755 instead of 766. There's probably no reason to make the file writable by everyone on the system.