LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   problem after moving ksh script from windows to linux (https://www.linuxquestions.org/questions/linux-newbie-8/problem-after-moving-ksh-script-from-windows-to-linux-778931/)

aamerjavaid 12-30-2009 01:54 PM

problem after moving ksh script from windows to linux
 
Hi All,

I am facing a strange problem.

My simple problem is: i ftp my running ksh script from linux to windows and then to other linux box.

But my script is not working showing different errors.

Please let me know the solution and pls also how to coz i am have been just moved to this section.?

Thanks.

Aamer

jschiwal 12-30-2009 02:11 PM

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.

aamerjavaid 12-30-2009 03:19 PM

Hi jschiwal,

Thanks for reply.

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 (Post 3809187)
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.


jschiwal 12-31-2009 04:45 PM

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.


All times are GMT -5. The time now is 06:36 AM.