Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This 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!


  Search this Thread
Old 01-24-2012, 01:25 AM   #1
Registered: Sep 2011
Posts: 55

Rep: Reputation: Disabled
confusion with Shells...


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.

Old 01-24-2012, 01:44 AM   #2
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
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.
Old 01-24-2012, 01:45 AM   #3
LQ Guru
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295

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,

Old 01-24-2012, 08:11 AM   #4
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,826

Rep: Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973
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.)

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:

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.

Last edited by David the H.; 01-24-2012 at 08:32 AM. Reason: as stated
Old 01-24-2012, 08:17 PM   #5
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,406

Rep: Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396
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.
Old 01-25-2012, 12:33 AM   #6
Registered: Sep 2011
Posts: 55

Original Poster
Rep: Reputation: Disabled
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
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
 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:
flag=$(awk -F"|" '{ if( '$COL_NUMBER' ~ /^ *$/ ){ printf("1"); }}' $file )
Lemme know what needs to be done how I can make it work.


Last edited by sukhdip; 01-25-2012 at 12:39 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Can all shells read all environmental variables set by other shells? carlodelmundo Linux - Newbie 2 07-23-2010 02:03 PM
confusion regarding the login shell and consequent sub-shells daudiam Linux - Newbie 2 06-13-2010 03:14 AM
do I need all the shells? helpme0904 Fedora 6 10-01-2004 02:03 PM
Why so many shells? linuxzouk Linux - Newbie 5 06-10-2004 07:36 AM
Shells andrewtc Linux - Newbie 3 05-01-2003 03:52 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:11 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration