TEST Command

Hello,

I need help with a test command.

Here is what I am trying to do :

I've got an interactive script that check if configuration files exist on 2 different directories, then all configuration files are print on screen by a short name.

My problem is when you type a "wrong name" or "enter" instead of one the short name given.

How can I get create a test that will check to see if you have exactly choosen one of the configuration files given ?

lst_proto=$(cat ${rep_config}/*.cfg | grep "#name_transfert#" | cut -d " " -f"2" | sort)
        echo "${lst_proto} "

                lst_proto_old=$(cat ${rep_config}/_HISTO_FIC_CFG/*.cfg | grep "#name_transfert#" | cut -d " " -f"2" | sort)
                if [ -z ${nb_proto_old} ];
        then
                echo " "
                echo "--------OLD PROTOTYPE-------------"
                echo " "
                echo "${lst_proto_old} "
                echo " "
        fi

        echo "Please choose your configuration file : \c"

        read prototype

        echo "-------------------------------------------"
        echo "You have choosen : ${prototype} \n"
        echo "-------------------------------------------"
        echo " "
#------------------------complet name files------------------
        cd ${rep_config}
        nom_cfg=$(${prototype}.cfg)

                if [ "#"${nom_cfg}"#" == "##" ];
                then
#------------------------OLD complet name files----------------------------
                        rep_config="${rep_config}/_HISTO_FIC_CFG"
                        cd ${rep_config}
                        nom_cfg=$(${prototype}.cfg)
                fi

I don't even know if it's possible.

Thanls for your help

Just do a string comparison between ${prototype} and each of the config names, if its wrong then return to the top of the function.

HI,

I try to compare ${prototype} with ${lst_proto} and ${lst_proto_old} but that doesn't work ! any idea ?

Thanks

after you perform the line "lst_proto=$(cat ${rep_config}/*.cfg | grep "#name_transfert#" | cut -d " " -f"2" | sort)"

do an "echo ${lst_proto}"

This variable may have more than one result in it which means you will have to iterate through and compare each result against ${prototype}

Hi robfwauk,

sorry for the delay, I was away few days.

Thanks for your help, but do you know how I could iterate through and compare each result against ${prototype} ?

I do not know how I could do this ...

Thanks.

This is an example of how you would iterate through a list of items:

for item in ${list_of_items}; do
    if [[ $item = teacup ]]; then
         echo "Five bucks for the $item"
    else
         echo "That $item is not for sale, sorry"
    fi
done

Apply it to your circumstance.

Have you considered using a select statement:

PS3="Please choose your configuration file :"
select prototype in $(....your code here....)
do
   if [[ -n $prototype ]]
   then
        echo "You selected $prototype"
        break;
    else
        echo "Invalid choice: $REPLY"
    fi
done

Hello,

I can't figure this out ... I need help please.

How to write a test that will read and check the content of $lst_proto and $lst_proto_old and see if the choice of $prototype exist in $lst_proto or in $lst_proto_old ?

Otherwise I need the script to say that the choice selected is not valid.

thanks

#!/usr/bin/ksh
lst_proto=/whatever/your_list
lst_proto_old=/whatever/your_list.old
echo "Enter your prototype:"
read prototype
RES=$(grep -l "$prototype" ${lst_proto} ${lst_proto_old} 2>/dev/null)
[[ -z "RES" ]] && echo "Prototype not valide" || echo "Prototype can be found in $RES"

Hi,

sorry ctsgnb, but the code given doesn't work.

$lst_proto and $lst_proto_old are list of configuration files. That means $lst_proto and $lst_proto_old may have some multiple choice.

ex :

$lst_proto = 
sonic
meag
$lst_proto_old =
ninte
mario

How to write a test that could read $lst_proto and $lst_proto_old to check if $prototype exist at least on one of the variables, otherwise exit.

Thanks for your help

---------- Post updated at 05:13 AM ---------- Previous update was at 05:04 AM ----------

Hi,

as I read some other posts, it seems that a case statement usually works best to check the content of variables. Does anyone can say somethings about it ?

---------- Post updated at 06:13 AM ---------- Previous update was at 05:13 AM ----------

Hello,

I do not know if what I've done is the way that it should be done but I solved my problem.

I have replaced $lst_proto and $lst_proto_old by temporary files. Here is what I have done. It could help someone ... Do not hesitate to comment my work ...

cat ${dir_config}/*.cfg | grep "#name_transfert#" | cut -d " " -f"2" | sort | tee ${fic_lst_proto}

nb_proto_old=$(ls ${dir_config}/_HISTO_FIC_CFG/*.cfg | wc -l )

        if [ -z ${nb_proto_old} ];
        then
                echo " "
                echo "--------OLD PROTOTYPE-------------"
                echo " "
                cat ${dir_config}/_HISTO_FIC_CFG/*.cfg | grep "#name_transfert#" | cut -d " " -f"2" | sort | tee ${fic_lst_proto_old}
                echo " "
         fi

                echo "Please choose your configuration file : \c"

                read prototype

                echo "-------------------------------------------"
                echo "You have choosen : ${prototype} \n"
                echo "-------------------------------------------"
                echo " "

           if [ `cat ${fic_lst_proto} | grep ${prototype}` ] || [ `cat ${fic_lst_proto_old} | grep ${prototype}` ]
           then

           else
                     echo "\n Error while reading configuration file!\n\n"
                     exit
           fi


Thanks all for your help

Your code could be enhanced a lot ... but we would need a bit more clue about the cfg file you have.
Could you please post the result of the following commands :

ls ${dir_config}/_HISTO_FIC_CFG/*.cfg
ls ${dir_config}/*.cfg
cat ${dir_config}/_HISTO_FIC_CFG/*.cfg
cat ${dir_config}/*.cfg

Hello,

As requested, here is the result :

ls ${dir_config}/_HISTO_FIC_CFG/*.cfg

NATIVE_HIN_SATURNE.cfg
NATIVE_SONIC.cfg
NATIVE_TAR_PLUTON.cfg
VITRINE_GL4_SATURNE.cfg
VITRINE_HM_PLUTON.cfg
VITRINE_PLUTON.cfg
ls ${dir_config}/*.cfg

NATIVE_HIN_MERCURE.cfg
NATIVE_MERCURE.cfg
NATIVE_TAR_CELCIUS.cfg
VITRINE_GL4_GRID.cfg
VITRINE_HM_SUN.cfg
VITRINE_SU2P4V1.cfg
cat ${dir_config}/_HISTO_FIC_CFG/*.cfg
cat ${dir_config}/*.cfg


#tronc_staf# /OPERATIONNEL/SUN

#branche# ALLEGES_BEST
#branche# TRACES
#nbr_feuille_par_#branche# PHYSICAL_MODEL/FORECAST 98

#branche#supprime# RESTART_SAT
#brancheSpecifique# RESTART_SAT RESTARTICE RESTARTICE
#brancheSpecifique# RESTART_SAT RESTART RESTART

The content of the configuration files do not change much. All info needed is between #tronc_staf# and #branche#.

So far I am just trying to simplify the first part of the script and make a strong condition that send warning or exit if the configuration file select is not a the list or if you type ENTER instead of selecting a file print on screen.

Info :

I do not need

#branche#supprime# RESTART_SAT

and

NOTE : for #brancheSpecifique# I need the last part of the line

#brancheSpecifique# RESTART_SAT RESTART RESTART : I am keeping the last RESTART

Thanks

Could you please explain as clearly as possible what you are trying to achieve ?

So you have many config files like

${dir_config}/_HISTO_FIC_CFG/.cfg
${dir_config}/
.cfg

So you want to be prompted for a prototype name, and then what you want the scripts to do ?

Do you want it to display the name of the config file in which the prototype has been found ?

echo "Enter prototype name:"
read prototype
grep -l "$prototype" ${dir_config}/_HISTO_FIC_CFG/*.cfg ${dir_config}/*.cfg