Solved

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

Posted on 2007-04-06
12
1,211 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
 
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

747 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

11 Experts available now in Live!

Get 1:1 Help Now