Solved

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

Posted on 2007-04-06
12
1,216 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
  • 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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 250 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sudo logging 5 90
Shell script errors 10 129
Bash Script to Analyze Oracle Schemas 11 102
awk sed variable in file 3 34
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…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

816 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now