LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-24-2017, 11:12 AM   #1
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Rep: Reputation: Disabled
Unable to set a variable for loop


Hi , i am trying to set a veriable for a loop depending on user choice , but somehow i am unable to figure out why i get :
Quote:
./test: line 8: 1t=1: command not found
./test: line 9: 2t=1 2: command not found
./test: line 10: 3t=1..3: command not found
./test: line 11: 4t=1..4: command not found
./test: line 12: 5t=1..5: command not found
./test: line 13: 6t=1..6: command not found
./test: line 14: 7t=1..7: command not found
./test: line 15: 8t=1..8: command not found
./test: line 16: 9t=1..9: command not found
./test: line 17: 10t=1..10: command not found
on this script .
Quote:
#!/bin/bash
lp(){
for i in "$var"
do
echo $i
done
}
1t="1"
2t="1 2"
3t="1..3"
4t="1..4"
5t="1..5"
6t="1..6"
7t="1..7"
8t="1..8"
9t="1..9"
10t="1..10"

echo -n "select 1 to 10 ."
read -r sel
case $sel in
1)
var="$1t"
lp
;;
2)
var="$2t"
lp
;;
3)
var="$3t"
lp
;;
4)
var="$4t"
lp
;;
5)
var="$5t"
lp
;;
6)
var="$6t"
lp
;;
7)
var="$7t"
lp
;;
8)
var="$8t"
lp
;;
9)
var="$9t"
lp
;;
10)
var="$10t"
lp
;;
*)
echo "Only a number between 1 and 10 is available"
exit 1
;;
esac
 
Old 07-24-2017, 11:16 AM   #2
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
As far as I aware, Bash shell variable names must start with a letter or an underscore, and thus not a digit.
 
1 members found this post helpful.
Old 07-24-2017, 11:32 AM   #3
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
Yup you are right .
However it is interesting that when i set a $var for the number of times i want to loop , the
function :

Quote:
for i in $var
do
echo $i
done
does not accept the current variable !!
anyone have a idea why ?

here it is the code :

Quote:
#!/bin/bash
lp(){
for i in $var
do
echo $i
done
}
t1="1"
t2="{1 2}"
t3="{1..3}"
t4="{1..4}"
t5="{1..5}"
t6="{1..6}"
t7="{1..7}"
t8="{1..8}"
t9="{1..9}"
t10="{1..10}"

echo -n "select 1 to 10 ."
read -r sel
case $sel in
1)
var="$t1"
lp
;;
2)
var="$t2"
lp
;;
3)
var="$t3"
lp
;;
4)
var="$t4"
lp
;;
5)
var="$t5"
lp
;;
6)
var="$t6"
lp
;;
7)
var="$t7"
lp
;;
8)
var="$t8"
lp
;;
9)
var="$t9"
lp
;;
10)
var="$t10"
lp
;;
*)
echo "Only a number between 1 and 10 is available"
exit 1
;;
esac
 
Old 07-24-2017, 12:07 PM   #4
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
Well , i did a quick search on the web for this issue and i found a solution to get around it .

Code:
#!/bin/bash
lp(){
for i in $(seq "$var")
do
echo $i
done
}
t1="1"
t2="2"
t3="3"
t4="4"
t5="5"
t6="6"
t7="7"
t8="8"
t9="9"
t10="10"

echo -n "select 1 to 10 ."
read -r sel
case $sel in
1)
var="$t1"
lp
;;
2)
var="$t2"
lp
;;
3)
var=$(t3)
lp
;;
4)
var="$t4"
lp
;;
5)
var="$t5"
lp
;;
6)
var="$t6"
lp
;;
7)
var="$t7"
lp
;;
8)
var="$t8"
lp
;;
9)
var="$t9"
lp
;;
10)
var="$t10"
lp
;;
*)
echo "Only a number between 1 and 10 is available"
exit 1
;;
esac

Last edited by pedropt; 07-24-2017 at 01:54 PM.
 
Old 07-24-2017, 12:56 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Kindly use [code] instead of [quote]:

Code:
The rigth way
Quote:
Not this
 
Old 07-24-2017, 02:15 PM   #6
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
As noted by others, please place your code snippets inside [CODE]...[/CODE] tags (not QUOTE tags), for better readability. You may type those yourself or click the "#" button in the edit controls.

In your for loop, $var is expanded as a string, not evaluated as a range specification. Similarly, your assignments, t1-10 are just strings and do not generate the ranges.

You need to rethink completely how that works.

As your loop always begins at 1, maybe you should pass it a limit value instead and perhaps use a while loop which breaks when the limit s reached.

Either way, I would refer you to man bash Compound commands for a better understanding of how those loop constructs actually work.
 
Old 07-24-2017, 03:43 PM   #7
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by pedropt View Post
Well , i did a quick search on the web for this issue and i found a solution to get around it .

Code:
#!/bin/bash
lp(){
for i in $(seq "$var")
do
echo $i
done
}
t1="1"
t2="2"
t3="3"
t4="4"
t5="5"
t6="6"
t7="7"
t8="8"
t9="9"
t10="10"

echo -n "select 1 to 10 ."
read -r sel
case $sel in
1)
var="$t1"
lp
;;
2)
var="$t2"
lp
;;
3)
var=$(t3)
lp
;;
4)
var="$t4"
lp
;;
5)
var="$t5"
lp
;;
6)
var="$t6"
lp
;;
7)
var="$t7"
lp
;;
8)
var="$t8"
lp
;;
9)
var="$t9"
lp
;;
10)
var="$t10"
lp
;;
*)
echo "Only a number between 1 and 10 is available"
exit 1
;;
esac
Isn't that just an incredibly long and convoluted way of doing this:
Code:
if [[ ($# == 1) && ($1 =~ [0-9]?[0-9]) && ($1 -le 10) ]]; then
   seq $1
else
   echo "Only a number between 1 and 10 is available"
fi

Last edited by suicidaleggroll; 07-24-2017 at 03:45 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to define a variable In a while loop? fatsheep Programming 16 07-30-2014 01:12 PM
[SOLVED] [BASH] non-empty variable before loop end, is empty after exiting loop aitor Programming 2 08-26-2010 09:57 AM
Unable to set environmental variable ssuhaib Linux - Newbie 2 04-08-2009 05:47 AM
incrementing variable in an until loop jeffreybluml Programming 2 05-12-2005 07:45 AM
getting a variable out of a WHILE loop. fhinkle Programming 8 02-22-2005 04:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration