Home > OS >  bash optional command in variable
bash optional command in variable

Time:02-08

i have a code:

L12(){
echo -e "/tftpboot/log/archive/L12/*/*$sn*L12*.log /tftpboot/log/diag/*$sn*L12*.log"
command="| grep -v hdd"
}
getlog(){
echo $(ls -ltr $(${1}) 2>/dev/null `${command}` | tail -1)
}

however $command does not seem to be inserting | grep -v hdd correctly i need $command to be either empty or | grep is there a simple solution to my issue or should i go for different approach

edit:

there may be another problem in there

i am loading a few "modules"

EVAL.sh

ev(){
case "${1}" in
*FAIL*) paint $red "FAIL";;
*PASS*) paint $green "PASS";;
*)echo;;
esac
result=${1}
}

rackinfo.sh (the "main script")

#! /bin/bash
#set -x
    n=0
    for src in $(ls modules/)
        do
            source modules/$src && ((n  ))
## debugging
# source src/$src || ((n  )) || echo "there may be an issue in $src"
    done
## debugging
# x=($n - $(ls | grep src | wc -l))
# echo -e "$x plugin(s) failed to laod correctly"
# echo -e "loaded $n modules"
########################################################################
command=cat
tests=("L12" "AL" "BI" "L12-3")
while read sn
        do
                paint $blue "$sn\t"
                for test in ${tests[@]}
                        do
                                log="$(ev "$(getlog ${test})")"
                                if [[ -z ${log} ]]
                                        then
                                                paint $cyan "${test} "; paint $red "!LOG "
                                else
                                                paint $cyan "${test} ";echo -ne "$log "

                                fi
done
echo
done <$1

the results i get are still containing "hdd" for L12()

CodePudding user response:

Set command to cat as a default.

Also, it's best to use an array for commands with arguments, in case any of the arguments is multiple words.

There's rarely a reason to write echo $(command). That's essentially the same as just writing command.

#default command does nothing
command=(cat) 

L12(){
    echo -e "/tftpboot/log/archive/L12/*/*$sn*L12*.log /tftpboot/log/diag/*$sn*L12*.log"
    command=(grep -v hdd)
}

getlog(){
    ls -ltr $(${1}) 2>/dev/null | "${command[@]}" | tail -1)
}
  •  Tags:  
  • Related