Results 1 to 2 of 2
Hi,
I have problem to use an alias that defined in ksh93 script, in the
functions in the same script onm Linux.
I definied an alias in main:
alias echo='echo ...
- 04-04-2011 #1Just Joined!
- Join Date
- Nov 2008
- Posts
- 6
problem to use an alias in the ksh functions
Hi,
I have problem to use an alias that defined in ksh93 script, in the
functions in the same script onm Linux.
I definied an alias in main:
alias echo='echo -e'
in order that echo will read backslashes
but when i executed it in function, the alias didnt work, and
performewd a regular echo, without -e
------------------------
cat test.ksh
#!/bin/ksh
alias echo='echo -e'
checkUsage
checkUsage()
{
echo "\ntest\n"
}
---------------------
> ./test.ksh
\nTest\n
- 04-04-2011 #2Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
The code you posted will not run in most Bourne-like shells that I know of because you call a function before you define it:
Assuming we correct that, then I get for script "s1":Code:% ./user1 ./user1[5]: checkUsage: not found [No such file or directory]
producing:Code:#!/usr/bin/env ksh # @(#) s1 Demonstrate ksh alias. # Utility functions: print-as-echo, print-line-with-visual-space, debug. pe() { for i;do printf "%s" "$i";done; printf "\n"; } pl() { pe;pe "-----" ;pe "$*"; } db() { ( printf " db, ";for i;do printf "%s" "$i";done; printf "\n" ) >&2 ; } db() { : ; } C=$HOME/bin/context && [ -f $C ] && . $C pe set -vx alias echo='echo -e' checkUsage() { echo "\ntest\n inside calling function" } checkUsage echo "\ntest\n outside calling function" printf "\ntest\n outside calling function (printf)\n" echo '\ntest\n outside calling function'
This seems to work for me.Code:% ./s1 Environment: LC_ALL = C, LANG = C (Versions displayed with local utility "version") OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64 Distribution : Debian GNU/Linux 5.0.7 (lenny) ksh 93s+ alias echo='echo -e' + alias echo='echo -e' checkUsage() { echo "\ntest\n inside calling function" } checkUsage + checkUsage + echo -e '\ntest\n inside calling function' test inside calling function echo "\ntest\n outside calling function" + echo -e '\ntest\n outside calling function' test outside calling function printf "\ntest\n outside calling function (printf)\n" test outside calling function (printf) + printf '\ntest\n outside calling function (printf)\n' echo '\ntest\n outside calling function' + echo -e '\ntest\n outside calling function' test outside calling function
It shows some of the variations that could be tried. The code that sets "-vx" is often useful to see details of what the shell is doing, which is not always what you think it does. It can be more difficult to read the results, however, because the output caused by "-vx" is written to stderr, and may or may not appear where you expect it with the other output -- you'll get it, but the order can be confusing.
Note that you will often see advice to use functions instead of aliases, and to use printf instead of echo. See the function definitions in the script for "pe", a replacement for echo that I use.
Best wishes ... cheers, drlWelcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )


Reply With Quote