2

I have data that's held in one of three directories and I wanted to create a loop that would allow a user to easily access the data.

I'm trying to make this loop work so that if someone enters 1-3 it reads the number and changes the variable timePeriod to either "daily", "bars10s", or "bars100ms". When I echo the timeCheck and timePeriod variables when running the code to see if they're ok, the timeCheck always comes back right, but the timePeriod reads "daily" no matter what the timeCheck variable reads as. I'm pretty new to bash, so any help would be appreciated.

#!/bin/bash

echo 

timePeriod=""
timeCheck=0

read -p "Time Period for fac to run(press 1 for daily, 2 for 10 sec 
intervals, 3 for 100ms intervals): " -e -i "$timeCheck" timeCheck

if [ $TimeCheck==1 ] ; then
    timePeriod="daily"
elif [ $TimeCheck==2 ] ; then
    timePeriod="bars10s"    
elif [ $TimeCheck==3 ] ; then
    timePeriod="bars100ms"
else
    echo "Not Valid Time Period. Please re-try."
fi


echo $timeCheck
echo $timePeriod

3 Answers 3

3

You can use select - for the menus like this.

PS3="Select what you want>"
select answer in "Daily period" "10 sec period" "100 ms" "exit program"
do
case "$REPLY" in
    1) timePeriod="daily" ; break;;
    2) timePeriod="10s" ; break;;
    3) timePeriod="100ms" ; break;;
    4) exit ;;
esac
done

echo "$timePeriod"
Sign up to request clarification or add additional context in comments.

2 Comments

thanks again for this select menu idea. It's working much better than what I was doing before.
I'm happy to help. Enjoy :)
2

You need to put spaces around conditions and boolean operators:

if [ $TimeCheck == 1 ] ; then

However, since it is an arithmetic test, it might be better doing:

if (( $TimeCheck==1 )) ; then

spaces are less important with arithmetic checks.

3 Comments

if his bash supports [[ .. ]], I would recommend to use that.
If [[ is supported, it's likely that (( is supported as well, which is preferable if you know you are doing arithmetic.
@Kent: my general rule is: use [[ .. ]] for pattern matching, (( .. )) for arithmetic comparisons, and [ .. ] only when you want to play tricks.
2

I see two errors:

  1. You need spaces in your if clause
  2. You use sometimes caps and somstimes not

The following is working:

#!/bin/bash

echo

timePeriod=""
timeCheck=0

read -p "Time Period for fac to run(press 1 for daily, 2 for 10 sec
intervals, 3 for 100ms intervals): " -e -i "$timeCheck" timeCheck

if [ $timeCheck == 1 ] ; then
    timePeriod="daily"
elif [ $timeCheck == 2 ] ; then
    timePeriod="bars10s"
elif [ $timeCheck == 3 ] ; then
    timePeriod="bars100ms"
else
    echo "Not Valid Time Period. Please re-try."
fi

echo $timeCheck
echo $timePeriod

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.