LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Read contents of file and execute command (https://www.linuxquestions.org/questions/programming-9/read-contents-of-file-and-execute-command-98623/)

cosiek 09-30-2003 10:22 AM

Read contents of file and execute command
 
I have a file named PKGSEL with on line like this:

"Var1" "Var2" "Var3" "Var4"

I don't know how to do something like this: if PKGSEL file contains "Var1" then the script should execute a command, if it contains "Var2" is should execute another command - and so on...

mfeat 09-30-2003 10:39 AM

VAR=`cat PKGSEL`
if [ "$VAR" = "Var1" ];

mfeat 09-30-2003 10:41 AM

VAR=`cat PKGSEL`
if [ "$VAR" = "Var1" ]; then
command1
elif [ "$VAR" = "Var2" ]; then
command2
elif [ "$VAR" = "Var3" ]; then
command3
fi

mfeat 09-30-2003 11:07 AM

If there's more than one "Var" on the line in the file this will work, also if the quotes are actually in the file this handles it:

do_cmd()
(
if [ "$1" = "Var1" ]; then
echo command1
elif [ "$1" = "Var2" ]; then
echo command2
elif [ "$1" = "Var3" ]; then
echo command3
fi
)
list_vars()
{
pcount=$#
i=1
while [ $i -le $pcount ]; do
do_cmd $1
shift
(( i = i + 1 ))
done
}
list_vars `cat PKGSEL | sed 's/"//g'`


All times are GMT -5. The time now is 08:21 PM.