?
Solved

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

Posted on 2007-04-06
12
Medium Priority
?
1,225 Views
Last Modified: 2012-06-27
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!
0
Comment
Question by:roschera
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
  • +1
12 Comments
 
LVL 58

Expert Comment

by:amit_g
ID: 18866590
On this file do

dos2unix YourFileName.sh

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

Author Comment

by:roschera
ID: 18866788
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!
0
 
LVL 6

Expert Comment

by:DocGyver
ID: 18866975
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:DocGyver
ID: 18867050
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.
0
 

Author Comment

by:roschera
ID: 18867239
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!
0
 
LVL 58

Expert Comment

by:amit_g
ID: 18867642
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.
0
 
LVL 6

Expert Comment

by:DocGyver
ID: 18867665
bowing out.  amit has this one.
0
 

Author Comment

by:roschera
ID: 18867767
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!
0
 
LVL 58

Accepted Solution

by:
amit_g earned 1000 total points
ID: 18868079
Create a new script (from scratch) kshtest1.sh and put this in it

#Begin
ls -l $1 $2
#end

then do

chmod +x kshtest1.sh

and execute

kshtest1.sh . ..

Does that work? If add this as very first line

#!/bin/ksh
#Begin
ls -l $1 $2
#end

does this work? If not, do this

echo '#!/bin/ksh' > kshtest3.sh
echo 'ls -l $1 $2' >> kshtest3.sh
chmod +x kshtest3.sh
kshtest3.sh

does this work?
0
 

Author Comment

by:roschera
ID: 18871459
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!
0
 
LVL 58

Expert Comment

by:amit_g
ID: 18874493
#!/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).
0
 
LVL 48

Expert Comment

by:Tintin
ID: 18879578
#! \bin\ksh

Note the incorrect use of backslashes.
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question