Solved

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

Posted on 2007-04-06
12
1,221 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 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
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…
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.

733 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