Find the answer to your Linux question:
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just 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

  2. #2
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,294
    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:
    Code:
    % ./user1
    ./user1[5]: checkUsage: not found [No such file or directory]
    Assuming we correct that, then I get for script "s1":
    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'
    producing:
    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
    This seems to work for me.

    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, drl
    Welcome - 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 )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •