Dazed and confused...need help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Dazed and confused...need help
# 1  
Old 01-15-2019
Dazed and confused...need help

I have one script - two versions; these are test scripts to solve my problem/issue.
The difference between the two scripts is that the "for....loop" in script 'loop1' uses a variable ($xlim) to control the loop; whereas in script 'loop2' the loop parameter are fixed integers.

'loop1'
-------------------------------------------------------
Code:
#!/bin/bash

zz=3
yy=0
xlim=5

for i in {1..$xlim}; do 
   echo "I = $i"
   ((yy=$zz\*i))
   echo "Welcome $i times  $yy"
done

-------------------------------------------------------------
OUTPUT:
===================================
Code:
$ loop1
I = {1..5}
./loop1: line 9: ((: {1..5}: syntax error: operand expected (error token is "{1..5}")
Welcome {1..5} times  0

===================================

'loop2'
------------------------------------------------------------
Code:
#!/bin/bash

zz=3
yy=0
xlim=5

for i in {1..5}; do 
   echo "I = $i"
   ((yy=$zz\*i))
   echo "Welcome $i times  $yy"
done

---------------------------------------------------------------------

OUTPUT
=====================================
Code:
$ loop2
I = 1
Welcome 1 times  3
I = 2
Welcome 2 times  6
I = 3
Welcome 3 times  9
I = 4
Welcome 4 times  12
I = 5
Welcome 5 times  15

=====================

Of course, my actual script needs a 'parametric loop' like in script 'loop1'.
So why if one of the loop parameters is not a 'static' (constant) value does "I = {1..5}" and not "I = 1", i.e why does $i substitute to "{1..5}"?

I have not been able to find any help otherwise and would appreciate any advice!

Last edited by vgersh99; 01-15-2019 at 06:14 PM..
# 2  
Old 01-15-2019
if we look into man bash and search for Brace Expansion:
Code:
       A correctly-formed brace expansion must contain unquoted opening and closing braces, and at least one unquoted comma or  a
       valid sequence expression.  Any incorrectly formed brace expansion is left unchanged.  A { or , may be quoted with a back‐
       slash to prevent its being considered part of a brace expression.  To avoid conflicts with parameter expansion, the string
       ${ is not considered eligible for brace expansion.

One possible workaround could be:
Code:
#!/usr/bin/bash
#set -x

zz=3
yy=0
xlim=5

for i in $(eval echo {1..${xlim}}) ; do
   echo "I = $i"
   ((yy=zz * i))
   echo "Welcome $i times  $yy"
done

# 3  
Old 01-15-2019
{1..9} just doesn't work that way. You can't stuff numbers into it.

How about a real loop:

Code:
for ((i=1; i<=xlim; i++))
do
...
done

or the old fashioned way:
Code:
i=1
while [ "$i" -lt "$xlim" ]
do
...
        i=$((i+1))
done


Last edited by Corona688; 02-08-2019 at 12:07 PM.. Reason: typo
These 2 Users Gave Thanks to Corona688 For This Post:
# 4  
Old 01-15-2019
The observed behaviour can be understood by carefully reading the man page, e.g. man bash:


Quote:
EXPANSION
...
The order of expansions is: brace expansion; tilde expansion, parameter and variable expansion, ...
...


Brace Expansion
Brace expansion is a mechanism by which arbitrary strings may be generated.

...

A sequence expression takes the form {x..y[..incr]}, where x and y are either integers or single characters, and incr, an optional increment, is an integer.
...

Brace expansion is performed before any other expansions, and any characters special to other expansions are preserved in the result.
...

A correctly-formed brace expansion must contain unquoted opening and closing braces, and at least one unquoted comma or a valid sequence expression. Any incorrectly
formed brace expansion is left unchanged.
As laid out, brace expansion is the first to take place, and it needs x and y to integers or characters, NOT variable references, which in turn make it fail and leave the construct unchanged (resulting in one single element (literal "{1..$xlim}") in the for loop and leading to the error message that you see in your post).


Others have shown before that you could use (the highly deprecated because inherently dangerous) evalto get the desired results, or e.g. use the seq command for your for loop:


Code:
$ for i in $(seq 1 5); do echo $i; done
1
2
3
4
5


Last edited by RudiC; 01-15-2019 at 07:03 PM..
This User Gave Thanks to RudiC For This Post:
# 5  
Old 01-15-2019
Corona668 THX for the advice; sometimes I forget to use "c" type constructs.
# 6  
Old 01-15-2019
You can't do that in bash however if you really need to do something like {1..5} then this is a workaround. Not recommended though...
Longhand using OSX 10.14.1, default bash terminal...
Code:
Last login: Tue Jan 15 22:54:14 on ttys000
AMIGA:amiga~> x={1..5}
AMIGA:amiga~> for y in $( eval "echo ${x}" ); do echo "$y"; done
1
2
3
4
5
AMIGA:amiga~> _

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Confused with if/then

Hi All, I'm pretty new to this so please bear with me... I'm trying to write a bash script to first search in a file for a string of characters; if the characters exist than skip the rest of the code until you get to the last line and run that command /sbdin/ldconfig; if the string doesn't... (2 Replies)
Discussion started by: gmdune
2 Replies

2. Ubuntu

New and Confused

Hello, I am having a problem with Dual Booting Windows XP Pro and Linux Mint. I have Three Hard Drives, One Hard Drive has Linux Mint Loaded on it. When it is hooked up to the computer by itself it works great. This is an IDE Drive. The Second Hard Drive has Window XP Pro loaded on it.... (3 Replies)
Discussion started by: Forextrading
3 Replies

3. Solaris

Confused c#t#d#s#

I am confused c#t#d#s# once I learn the following : slice 0 ...... 0 to 2520 slice 1....... 2521 to 2840 slice 6........2841 to 8891 slice 2........0 to 8891 really really confused. Please explain. (8 Replies)
Discussion started by: deltakutty
8 Replies

4. UNIX for Dummies Questions & Answers

Confused with the permission

Hi, One of the directory in my folder has the following persion : drwxr-sr-x Can any one explain this permission and how can his occur ? This folder is created with Win SCP. Any folder created with win SCP is being set to this permission only. (1 Reply)
Discussion started by: risshanth
1 Replies

5. Shell Programming and Scripting

sed very confused

Hello experts, I have this complicated code that output value in between pattern and add "a string" to the front of the output. The problems I have many pattern that are the same. so I want to know how to get the value between 1st pattern, 2nd pattern etc. Any suggestions? sed -n... (14 Replies)
Discussion started by: minifish
14 Replies

6. UNIX for Dummies Questions & Answers

confused

A red hat linux ftp server exists in which a file exists. My problem is I need to connect to this server from my windows xp terminal which is in the same network & retrieve the file then convert it to xcel for some data Pls advs commands and procedure to connect to the machine...oh my god... (1 Reply)
Discussion started by: sauravjung
1 Replies

7. Shell Programming and Scripting

confused with cp

may i know what cp $1 $2 $0 $2 does? (12 Replies)
Discussion started by: C|[anti-trust]
12 Replies

8. UNIX for Dummies Questions & Answers

confused

hi! how when i'm chattin inside com there was this chatter andi don't know what he did but he saw all my files inside my shell. what did he do? (4 Replies)
Discussion started by: hapiworm
4 Replies

9. UNIX for Dummies Questions & Answers

confused,,,,

Hi,,, is there any possibility to install Linux in my P.C which is use Win98 without loose anything from my hard disk???? ---------------------------------------------------------------------------------- Is it better for a newbie in this kind of OS to install Linux instead of... (5 Replies)
Discussion started by: spyros
5 Replies
Login or Register to Ask a Question