LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   confusion with Shells... (https://www.linuxquestions.org/questions/linux-newbie-8/confusion-with-shells-925390/)

sukhdip 01-24-2012 01:25 AM

confusion with Shells...
 
Hi

I need to understand as per my requirement Can I use scripts within two shells i.e., ksh and bash.

The scene is like this I have written some scripts in bash only. But later i came to know that I have to run those scripts in ksh shell only, as IBM AIX is running ksh.
Now many of my scripts fail to run. May be due to the syntax difference by ksh n bash or sh. Please clear me if this really due to ksh n bash syntax.?
And if Yes.(As I understand) then can I write few lines in script that:
1. Script will take temporary shell i.e., bash and run all the scripts.
2. After doing the work it will call back ksh shell.(Is it possible)
3. Finally the system will be running in ksh only.

As per I'm thinking can it be done?
Is the good way or harmful way as my server will be production server?
Will there any Shell and subshell issue rises within the variables?
OR I have to change the scripts to ksh env only. (That I don't know exactly)
Please provide me the best suggestions.
and help me how i can make this possible.

Thanks

catkin 01-24-2012 01:44 AM

Yes -- ksh is sufficiently different from bash that most bash scripts cannot be run by ksh.

Regards your proposed workaround, is bash available? If so, using a bash shebang line (#!/bin/bash)solves the problem. If not, the workaround is not feasible.

EricTRA 01-24-2012 01:45 AM

Hello,

Bash and KSH are different indeed. The fact that your scripts written for Bash don't work in KSH is because a lot of commands in AIX (as you indicated you're using) work differently when compared to regular Linux distros. Several commands that work in Linux don't have the same parameter options in AIX. If at the end your scripts will be used on AIX, then I advice you to write your scripts for that environment keeping in mind that KSH is the default shell on that system and adapting your scripts as such. If you're worried about testing them on your system, then install KSH on your system (for Debian: apt-get install ksh) but keep in mind that several commands on AIX have less or more parameters.

Kind regards,

Eric

David the H. 01-24-2012 08:11 AM

ksh and bash have borrowed heavily from each other, so there are a large number of features with identical or nearly-identical syntax, but they aren't completely the same.

For true portability (or at least the greatest chance for it), you need to keep your scripts to the POSIX specification. ksh, bash, and most other shells of bourne descent attempt to be posixly-compliant, and will run scripts in posix mode if a #!/bin/sh shebang is used. (Note however that this doesn't necessarily mean that they will reject non-posix syntax; it only ensures that a posixly-compliant script will run.)

http://www.qnx.com/developers/docs/6...html#POSIXMode

The above link is just the first version I found online. I suggest checking with your system's actual documentation. The next page is focused on bash and its extensions, but it also does a pretty good job summarizing what does and does not comprise posix syntax:

http://mywiki.wooledge.org/Bashism

Note too that many of the core utilities like sed and awk also have both posix-defined features and non-posix extensions, with the implementations on different platforms supporting different things. The gnu utilities used on Linux in particular have a large number of extensions not found elsewhere. You need to read the documentation carefully of all the tools you use and only choose the posix and/or cross-system options.


Edit: Just to make sure you know all your options:

1) Install bash on AIX, write all of your scripts with bash syntax, and run them with #!/bin/bash.

2) Install ksh on Linux, write all of your scripts with ksh syntax, and run them with #!/bin/ksh.

3) Write all of your scripts with posix-compliant syntax, and run them on either system using #!/bin/sh.

*) Whatever option you choose, be sure to check the options and syntax of all the external commands involved to ensure only cross-platform features are used.

chrism01 01-24-2012 08:17 PM

I'd also add that AIX will have cmds (not just options) that are different from Linux.
In short, to save yourself grief & time, get an acct on a dev/test AIX box and do it all in ksh, as per prod.
Any other approach is more effort UNLESS bash is avail/allowed on your(!) AIX system, which from your orig comment seems not.

sukhdip 01-25-2012 12:33 AM

Hi thanks guys for your valuable replies. I tried with ksh on my RHEL 5.5 system. its working fine.
But when I checked it with AIX system it's giving me error.
Yes I run using #!/bin/sh and also tried with #!/bin/ksh. output from both is same on RHEL system.
But giving a headache with AIX. Finally have to run on AIX and Can't replace ksh with bash on AIX.

I'm posting the specific line and error report. Please help me out
Error Code: 1
Code:

Syntax Error The source line is 1.
 The error context is
 ~ /^ *$/ ){ printf("1"); } ( $1
 awk: 0602-500 Quitting The source line is 1.

Error Code: 2
Code:

Syntax Error The source line is 1.
 The error context is
                >>> if <<< ( $1 ~ /^ *$/ ){ printf("1"); }
 awk: 0602-500 Quitting The source line is 1.

These both errors are coming in one line only.
Here is the line with awk spec:
Code:

flag=$(awk -F"|" '{ if( '$COL_NUMBER' ~ /^ *$/ ){ printf("1"); }}' $file )
Lemme know what needs to be done how I can make it work.

Thanks


All times are GMT -5. The time now is 10:51 AM.