converting ksh scripts to sh

Hello All,

I have a whole bunch of shell scripts written in a ksh environment and which successfully execute there. However, I found out that they eventually need to be used in a sh environment. So some commands like some_variable=$(some_command) fail because sh doesn't understand $(.....). I identified a list of scripts that have the $(.....) statements. Now I have to replace all those with `.....`

There must be someway I can automate this find and replace action right? Can you please point me in the right direction? Can I use sed for it? If so, what is the exact command? I can identify the statements using the regex '\$(.*)' but I don't know how to replace the command with `...`
I guess I have to parse each line of each file in a loop and do the substitution but I don't know the substituion command.
Thanks in advance for ur help.:slight_smile:

Or can you acquire/compile ksh for that environment?

thanks for ur response. unforunately, i am not in a position to change the ksh environment to sh although that would be wise to do :).

What will the deployed OS be? It may be that there is a POSIX shell in that environment that will handle most or this syntax, reducing the amount of changes you will need to do.

reborg, I'm just a shell programmer and the OS is solaris (which I access using putty from windows). Both sh and ksh seem to be installed (I can execute both from prompt). Should I use a C solution (pressed for time)? Thanks in advance.

Then why not use "ksh" if you have it?

To me it is madness to have working scripts, that then you have to rewrite just to use a different shell when they can be called as child processes by shell scripts of any other flavour.

If time is pressing why make what sound like tens or hundreds of changes that then require regression testing? It sounds like you are basically taking working production scripts and turning them into development projects.

So, you do the simple changes, then what about array variables and all the other ksh features?

Your trust and faith in these new scripts should be at rock bottom until they are fully tested, not exactly the thing to do if you are pressed for time.

wise counsel, porter. Thanks.

However, the scripts are for the 'production system' which apparently does not have ksh, while the development system does (Don't ask me why). That is why I am scrambling to convert them here. Most should be simple in that they don't use arrays. Only thing that I can think of is the issue with $(...) commands. Any other issue should be caught by the testing people - not expecting too many though. I had this shell differences thing in the back of my mind when I made the scripts but I guess I was not wary enough/ didn't have enough information about the differences.

What shells does it have?

Check for combinations of /bin/bash, /usr/bin/ksh, /usr/local/bin/ksh, /bin/zsh etc?

porter, I'm just a shell programmer working on the project on a need to know basis, unfortunately. The higher ups do not want to make any compromises on the production system, so they instructed me to change the $(...) statements. I guess I can write a small line parser in C that will do the job. Its all I can come up with for the moment. I'm sure there are much easier, better solutions for the subsitution but i don't know of any. maybe perl or ruby (both of which I do not know). I'll consult with my tech lead...perhaps i can catch hold of him.

/usr/xpg4/bin/sh

will allow $( ) and other POSIX constructs, it is probable that that will be installed, while it will not completely remedy your situation it might reduce the amount of work you need to do.

...but are happy to put hurried/untested scripts on? How are you going to regression test, do you have a pre-prod system that similarly doesn't have ksh on it?

Thanks for the insight and advice porter. Apparently they don't have the xpg4 sh (the scripts simply didn't work in the system). These scripts haven't been tested yet. Someone from testing did rudimentary testing and discovered that the scripts don't work. Now I've got to rectify it. Perhaps, I'll use some start-from-scratch logic like this..

In the algorithm below,
'conv' is used count number of $(..) to convert.
Not sure, if regular parantheses these ever occur (just plain '(....)') . Just realized this. I am not accounting for them. But let me know if you know that regular parathesis do occur. As far as i know i am not using grep statements searching for regular paranthesis in the scripts)

For each file from list of files.
do

Move file to file_ksh

For each line in file_ksh
do

For each character in line
do

if char is $,
if next char is (
conv = conv + 1
replace_char = `
else if char is ) and conv != 0
replace_char = `
conv = conv - 1
else
replace_char=char

Write replace character into line_variable.
done

write line_variable into file

done

done

How does this sound?? I know that its roundabout but I dont know any other way. Thanks.