echo in ksh sh & bash


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users echo in ksh sh & bash
# 1  
Old 01-27-2009
echo in ksh sh & bash

Hello,

I have lib file which contain a function that get text to print on screen by echo command.

Several scripts are inculde this lib and use this function.
Each one of them is written in different shell language (sh ksh & bash).
This causing some issues when using backslash charater as part of the test that sends to this function.
I would like to create two echo command which will executed according to the current shell.
How can I extarct the current shell which used by the calling script to the print function ?

Thanks a head
Alalush
# 2  
Old 01-27-2009
echo is usually a shell builtin - otherwise it is /usr/bin/echo.

I am not understanding what you want to do - override the echo builtin?
# 3  
Old 01-27-2009
Each shell command reads an environmental file before execution.

For sh, it's .profile
For ksh, it's the $ENV file.
For bash, it's the $BASH_ENV file.

For sh, the echo command is already /usr/bin/echo.

For ksh, you'll have to alias this command like so:

alias -x echo=/usr/bin/echo

or make it a function, I've seen aliases non-exported in some Solaris OS...


function echo
{
/usr/bin/echo $*
}



The same thing should work for bash.
# 4  
Old 01-27-2009
To answer the question: "${0}" should be the CURRENT shell name. You could try to get the version information as well (might be limited and I haven't tried that but you should be able to get this from the environment variables).
The calling process is your parent. You should never check your parent's property. If you need - then most probably the application needs an re-design.
My guess is that the parent process should call this code with a pre-formatted text like "{bold}this{red}is{normal}some text". Of course we should ask ourselves whether it is a good idea to use shell scripts here (probably you should use python/perl/some high level language).

To correct your approach: If you call a script then its executable should be specified in the first line (ex. #!/bin/ksh). If you have several scripts and every is written for a different shell - you should execute them and this would execute a new shell/whatever instance. Since every shell seems to be a separate entity (as it seems from your description) - you should either normalize them (re-write everything to the same script/shell/whatever) or just leave them as it is right now without using any common shell script file. There is nothing worse than using thousands of different languages in a single project maintained by thousands of developers.
If your script is called from other scripts (using several different shells to execute them) - then I cannot understand how this is a library script. Maybe this is a script that is executed with some options specified?

By the way: My English is not the the best but bellow you can find somewhat corrected version of your post (maybe someone else can correct it more? or even correct my post?). Sorry for being the fussy kind:

Quote:
Hello,

I have a lib file which contains a function that gets (receive would be better) text to print on to the screen /by/ using echo command.

Several scripts are inculded in this lib and /use/ are using this function.
Each one of them is written in a different shell language (sh ksh & bash).
This is causing some issues when using backslash character as part of the test that /sends to this function/ (Sorry, but I do not understand that part).
I would like to create two echo commands which /will/ would be executed according to the current shell.
How can I extarct the current shell which is used by the calling script /to the print function / to print the text output?

Thanks /a head/ in advance
Alalush
# 5  
Old 02-02-2009
mind bash

Alalush ,

- using escape sequences (eg. "\t") on shell strings works uniformly among most shells in the unix world (sh - the bourne shell , and ksh - korn shell) ;

- however, this is not true for linux and bash (bourne again shell) ;

-- in bash, you must use the "-e" flag on echo , in order to produce the same effect for the escape sequences ;

hope this helps.

good luck , and success !

botao
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

\n in ksh using echo & printf

#!/usr/bin/ksh var1="Hi World" var2="Morning" var3=$(echo "$var1" \n "$var2") echo $var3 var3=$(printf "$var1 \n $var2") echo $var3 Output Any way to get in my $var3 ? (7 Replies)
Discussion started by: dahlia84
7 Replies

2. Shell Programming and Scripting

combination between || and && in IF condition with ksh

Dear All, Please advice about this issue. when i run this line in a script if && || && || && if i enter $x = test3 and $y = test1 the If condition apply while it should not Best Regards (2 Replies)
Discussion started by: islam.said
2 Replies

3. Shell Programming and Scripting

Append && echo "success" to all commands

I am learning to build from SVN and other tools, with a lot of copying and pasting from forums. I like to append && echo "success" to all commands so that I can see at a glance if things went all right. Is there a way that I can have the bash shell append this to all commands? Thanks! (5 Replies)
Discussion started by: dotancohen
5 Replies

4. Shell Programming and Scripting

Bash & Ksh

I am using a Linux machine and it's default shell is BASH . I have korn shell script and inside it's setting environment variables. But after execuitng the script those env values are not holding the values . If I run the script outside of that ksh script with source a.sh , it's able to hold to... (2 Replies)
Discussion started by: talashil
2 Replies

5. Shell Programming and Scripting

Bash & Ksh

I am using a Linux machine and it's default shell is BASH . I have korn shell script and inside it's setting environment variables. But after execuitng the script those env values are not holding the values . If I run the script outside of that ksh script with source a.sh , it's able to hold to... (1 Reply)
Discussion started by: talashil
1 Replies

6. Shell Programming and Scripting

bash & Ksh loop problem

hi i was trying to optimize one script and i came across this problem .. i am putting some pseudo code here $ >cat a.sh ls | while read I do i=$(($i + 1)) done echo "total no of files : " $ >ksh a.sh total no of files : $ >bash a.sh total no of files : why is... (1 Reply)
Discussion started by: zedex
1 Replies

7. UNIX for Advanced & Expert Users

Echo with ksh shell

Hi All, I'm trying to use the "echo" command in a korn shell script, and I want it to drop the trailing newline. Now I know that with the bash shell, the "-n" flag would solve this issue. Does anyone know how this can be done with the korn shell? Cheers Khoom (10 Replies)
Discussion started by: Khoomfire
10 Replies

8. What is on Your Mind?

[[ $(date +%Y) == 2007 ]] && echo "Happy New Year"

Same as the Title! :) (2 Replies)
Discussion started by: ripat
2 Replies

9. Shell Programming and Scripting

what is ksh equivalent of bash echo -n ?

Hi folks, I need to stop printing a new line after echoing a string in KSH. i know bash provides echo -n "string" what is the ksh equivalent for this ? (3 Replies)
Discussion started by: mudhireddy
3 Replies

10. Shell Programming and Scripting

Exactly How can we define "sh ksh csh &bash"

Hi - I m prashant. I m new in UNIX&LINUX world. I want to ask that how can we define the shell in Linux like bash,ksh,csh in Linux. What is the use of these shells. I know there are mny experts on net if you can tell me then please do me this favour and tell me about this topic. ... (1 Reply)
Discussion started by: prashantsingh
1 Replies
Login or Register to Ask a Question