problem after moving ksh script from windows to linux
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Load the script in vim. If you see "^M" at the end of the lines, then run dos2unix to convert the script to use \n newlines instead of return&linefeed (\r\n).
You could use "head <filename> -n1 | od -c" to check the file manually. If you see "\r\n" at the end, you can use:
sed -i 's/\r//g' <filename>
to convert the script.
Also check the first line of the script. Does is start with "#!/usr/bin/ksh" or "#!/bin/env ksh". If not add this line. If your default shell is bash, you will see errors trying to run a ksh script.
Also make sure that the target system has ksh installed.
I loaded script in vim, didn't see any "^M" at the end of lines.
Then i run dos2unix to script. it converted successfully but when i tried running script, it abnormally exit my shell (no exit @ end of script).
then i tried option 2("head <filename> -n1 | od -c"). i saw "\r \n" at the end. i tried running sed -i 's/\r//g' <filename> to another copy of script and again my shell abnormally exited.
my script starts with "#!/usr/bin/ksh" but pls explain what is difference in these.
ksh is installed and my user's default shell is ksh.
Please help me out.
Thanks.
Aamer
Quote:
Originally Posted by jschiwal
Load the script in vim. If you see "^M" at the end of the lines, then run dos2unix to convert the script to use \n newlines instead of return&linefeed (\r\n).
You could use "head <filename> -n1 | od -c" to check the file manually. If you see "\r\n" at the end, you can use:
sed -i 's/\r//g' <filename>
to convert the script.
Also check the first line of the script. Does is start with "#!/usr/bin/ksh" or "#!/bin/env ksh". If not add this line. If your default shell is bash, you will see errors trying to run a ksh script.
Also make sure that the target system has ksh installed.
Since you mentioned uploading the scripts to a windows computer, I considered the possibility that windows line endings were inserted. This can happen if you edit a script in windows, and sometimes by copying a file to a windows filesystem. Scripts and Makefiles will fail when this is the case.
The first line of a script tells the kernel which shell to use to run it.
#!/usr/bin/ksh indicates the full path to the shell. Because one installation might locate the k shell elsewhere, another way of doing the same thing is: "#!/bin/env ksh" which will launch the ksh if it is in your path. Portable Unix scripts tend to be written this way. The downside is that this practice is less secure if the environment variables can be manipulated. Especially for Unix and some BSDs where SUID scripts are allowed.
One thing I would recommend is using tar to archive your scripts into a tarball. Then FTPing them to windows won't change the line endings or the permissions or ownerships of the scripts. Since Linux/Unix use UID & GID numbers, you may need to use the "chown" program to make them runnable. Tar preserves the permissions so "chmod" probably won't be necessary.
Which two Linux distro's are you using. Does one of them use selinux or apparmor? If so, it's possible that security attributes or setting differences between the two distro's may be the problem. Files or directories these scripts try to read and write from could be protected by the SELinux or AppArmor.
Other things to consider is if there is a difference between the versions of ksh that your two distro's use. If the source distro uses a newer version of ksh and these scripts use new features, this might be the problem.
Also what do these scripts do? Are they distro specific and try to read or modify files that the other distro doesn't use? Some linux distros use System V style booting & process scripts and others use BSD style.
You may need to tackle one error at a time. At least post a couple as examples, and provide more details on what distro, distro version and architecture used.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.