Link to home
Start Free TrialLog in
Avatar of roschera
roschera

asked on

Need help with korn shell script - it's erroring out on ls command

I'm using the public domain korn shell in the CYGWIN unix emulator.  I'm trying display the privileges of two different files where the user only has to run the script with the two file names as parameters.  This is what I've gotten so far.

#! \bin\ksh
export FILE_ONE=$1
export FILE_TWO=$2

ls -al $FILE_ONE
ls -al $FILE_TWO

I realize this is probably a very klunky way of doing this, but it's all I know how to do right now.  I'm receiving the following error with the above code:

testksh: line 5: $'\r': command not found
: No such file or directory

I've put in echo statements which shows that the parameter is being assigned the correct value.  I've also run the ls statement on it's own successfully with the variable.

Please help, I'm at a loss as what to do next.

Thanks!
Avatar of amit_g
amit_g
Flag of United States of America image

On this file do

dos2unix YourFileName.sh

and use Unix editor (from the shell) itself to edit these files. Or you may use Textpad.
Avatar of roschera
roschera

ASKER

Okay I've given the file an extension of .sh using textpad.  I'm no longer getting the command not found error, however, i'm still getting the No such file or directory error.  As stated before, when I run the ls statement from the command line with the parameter it works just fine, so what's next?

Thanks!
Another problem you could run into is if there are spaces in the file name.  You should put double quotes around the file names.  amit_g is right though.  If you created the script with notepad vs. say cygwin's VI command then you have embedded cr char(13) that will be removed by the dos2unix.

A simpler version of what you do would be:

---begin script
#!/bin/ksh
ls -al "$1" "$2"
---end script

and run it with something like:

scriptname "/my/deep/directory/with spaces/filename" "/my/deep/simplefile"

I'm assuming you are passing it standard paths like unix and not things like "C:\Documents and Settings\myname\myfile.txt"  If you want to pass it regular Windows type file names take a look at the cygwin command "cygpath" it can convert between the two
The error may stem from the file being in "dos" format rather than "unix" or might be because of spaces.  Unfortunately even if you use the quotes around the file name when you call the script you still need to use them again inside the script.
Hmmm....very interesting.  I wrote the script again from scratch using TextPad and I chose the UNIX file format when saving it with a .sh extension.  Now it's listing the file info, however, it's now issuing the following error message:

kshtest.sh: line 1: binksh: command not found

Line 1 - Cut and pasted here, not re-typed:
#!/bin/ksh

Any ideas?

Thanks!
Do

ls -l /bin/ksh

Does it exist? Also make sure that this is the first line with nothing before #.

The extension is not at as important in UNIX as it is in windows. You can name the file anything even without an extension and it would still work as well.
bowing out.  amit has this one.
I did

ls -l /bin/ksh

and the file does exist....and there is nothing before #!/bin/ksh....I'd love to be able to run this script without getting any errors....any help is appreciated.

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of amit_g
amit_g
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Amit_g,

Adding #Begin and #end and leaving #!/bin/ksh as the first line fixed the problem!   Did this behavior have something to do with the fact I'm running this with CYGWIN?

Thank you so much for seeing this one through!
#!/bin/ksh itself is not a problem unless you don't have ksh installed or there is some mistake in this line (like extra chars from DOS format).
#! \bin\ksh

Note the incorrect use of backslashes.