please help with shell script

Discussion in 'Programming/Scripts' started by alexus, Apr 19, 2007.

  1. alexus

    alexus New Member

    #!/bin/sh
    # $Id$
    #

    #

    sv()
    {
    for i in `echo $*`; do
    `echo $i | tr [a-z] [A-Z]`="`which $WFU $i ; if [ $? != 0 ]; then exit 1 ; fi`"
    done
    }

    #

    if [ -f /etc/redhat-release ]; then
    OS=REDHAT
    WFU="--skip-alias"
    fi

    sv which echo grep uname rsync

    ./_sir.sh: line 10: WHICH=/usr/bin/which: No such file or directory
    ./_sir.sh: line 10: ECHO=/bin/echo: No such file or directory
    ./_sir.sh: line 10: GREP=/bin/grep: No such file or directory
    ./_sir.sh: line 10: UNAME=/bin/uname: No such file or directory
    ./_sir.sh: line 10: RSYNC=/usr/bin/rsync: No such file or directory


    What i need is for shell to execute those lines but not execute, but make variable
     
  2. cfajohnson

    cfajohnson New Member

    That is an unsafe way to operate on a list of files. Use:

    Code:
    for i in *
    do
    
    Code:
    var=`printf "%s\n" "$i" | tr '[a-z]' '[A-Z]'`
    val=`which $WFU "$i"` || continue
    eval "$var=\$val"
    
     
  3. alexus

    alexus New Member

    i'm not sure why did you decided that i'm operating on list of files, which i'm not, i'm just simply passing all parameters from calling this function...
     
  4. cfajohnson

    cfajohnson New Member


    Because I misread `echo $*` as `echo *`.

    Let me rephrase it:

    That is an unsafe way to operate on a list of parameters. Use:

    Code:
    for i in "$@"
    do
    
    
     
  5. alexus

    alexus New Member

    and how is that safer then what i had? it might be a good idea to explain what this means :)
     
  6. cfajohnson

    cfajohnson New Member

    for i in `echo $*` is an inefficient way of writing for i in $*, which will break any arguments on whitespace (or whatever is contained in $IFS).

    For example, compare these two functions:

    Code:
    func1()
    {
       n=0
       for f in $*
       do
         n=$(( $n + 1 ))
         printf "Arg. no. %d: %s\n" "$n" "$f"
       done
    }
    
    func2()
    {
       n=0
       for f in "$@"
       do
         n=$(( $n + 1 ))
         printf "Arg. no. %d: %s\n" "$n" "fi"
       done
    }
    
    
    
    Now, compare the output of the two functions, each called with two arguments:

    Code:
    func1 "This is a test" "another test"
    echo
    func2 "This is a test" "another test"
    
    
    The output:

    Code:
    Arg. no. 1: This
    Arg. no. 2: is
    Arg. no. 3: a
    Arg. no. 4: test
    Arg. no. 5: another
    Arg. no. 6: test
    
    Arg. no. 1: This is a test
    Arg. no. 2: another test
    
    
     
  7. bakman

    bakman New Member

    Similar Question: using Shell with sed

    hello,
    I am new to shell and trying to write a script that does the following:
    Look in a directory and all its subdirectories for files that contain a specific string (call it text1). Any file that is found should be back up and then text1 replaced by another string (call it text2). The names of all the files that have been modified should be written to another output file.
    I found on the net that sed can be very helpful and can be help:
    sed 's/old/new/g' < IN > TMP && mv TMP IN

    Do you have an idea how I can use sed or any other library that can accomplish what i'm trying to do.
     
  8. falko

    falko Super Moderator Howtoforge Staff

Share This Page