LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > *BSD
User Name
Password
*BSD This forum is for the discussion of all BSD variants.
FreeBSD, OpenBSD, NetBSD, etc.

Notices


Reply
  Search this Thread
Old 04-07-2014, 09:21 AM   #1
anandg111
Member
 
Registered: Jan 2012
Posts: 56

Rep: Reputation: Disabled
Use of {}


for the block of code
Code:
servers=`echo Machine0{1,2,3,4,5,6,7,8,9}`
for i in $servers
do
echo "$i"
done
I am expecting answer
Code:
Machine01
Machine02
Machine03
Machine04
Machine05
Machine06
Machine07
Machine08
Machine09
but the answer i am getting is
Code:
Machine0{1,2,3,4,5,6,7,8,9}
On some of the systems it works well.
please advice.

Last edited by anandg111; 04-07-2014 at 09:22 AM.
 
Old 04-07-2014, 09:33 AM   #2
Soadyheid
Senior Member
 
Registered: Aug 2010
Location: Near Edinburgh, Scotland
Distribution: Cinnamon Mint 20.1 (Laptop) and 20.2 (Desktop)
Posts: 1,671

Rep: Reputation: 486Reputation: 486Reputation: 486Reputation: 486Reputation: 486
Code:
`echo Machine0{1,2,3,4,5,6,7,8,9}`
This is all I see you doing. I'm not a programmer but I don't think you've set up your indexing statements correctly.

Come to think of it... Take out the "echo" from the "servers=" statement and try again.

Play Bonny!


Last edited by Soadyheid; 04-07-2014 at 09:35 AM.
 
Old 04-07-2014, 09:42 AM   #3
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
This works for me.

Code:
^_^[sam@farcry:~]$ servers=`echo Machine0{1,2,3,4,5,6,7,8,9}`
^_^[sam@farcry:~]$ for i in $servers
> do
> echo "$i"
> done
Machine01
Machine02
Machine03
Machine04
Machine05
Machine06
Machine07
Machine08
Machine09
It's possible you have "set -o noglob" on your shell. To disable noglob for your current shell you could try...

Code:
set +o noglob
You're better off doing something like this.

Code:
for i in {1..9};do
  echo "Machine0${i}"
done
Bash pro tip: you should always quote variables.
 
Old 04-07-2014, 09:54 AM   #4
anandg111
Member
 
Registered: Jan 2012
Posts: 56

Original Poster
Rep: Reputation: Disabled
set +o noglob

I tried `set +o noglob` and the other solution you suggested but still getting the same result.
 
Old 04-07-2014, 10:06 AM   #5
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
What shell are you using?
 
Old 04-07-2014, 10:08 AM   #6
anandg111
Member
 
Registered: Jan 2012
Posts: 56

Original Poster
Rep: Reputation: Disabled
bourne shell

bourne shell
 
Old 04-07-2014, 10:27 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
So your output is as follows:
Code:
$ echo $SHELL
/bin/sh
 
Old 04-07-2014, 11:42 AM   #8
anandg111
Member
 
Registered: Jan 2012
Posts: 56

Original Poster
Rep: Reputation: Disabled
yes
 
Old 04-08-2014, 02:04 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
In that case, please perform the following and provide the output:
Code:
ls -l /bin/sh
 
Old 04-08-2014, 08:41 AM   #10
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
If you're truly using the bourne shell and not some variant of the bourne again shell (bash) then I don't believe that using brackets like that is available to you.

Code:
man 1 sh
If you read the man page it will tell you what is supported. After reading a bourne shell man page I found on the internet (because I don't have bourne shell in my system) the file name expansion doesn't support curly braces.
 
Old 04-08-2014, 09:00 AM   #11
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by anandg111 View Post
bourne shell
What Linux distribution are you using?
The (real) Bourne shell is very unlikely to be installed on your machine. It is generally emulated by clones like dash, bash and others.
bash supports the brace based syntax you are using but not dash which is likely what you use.
 
1 members found this post helpful.
Old 04-09-2014, 07:53 AM   #12
anandg111
Member
 
Registered: Jan 2012
Posts: 56

Original Poster
Rep: Reputation: Disabled
@grail ->

$ echo $SHELL
/bin/sh
$ ls -l /bin/sh
-r-xr-xr-x 1 0 0 147056 Oct 1 2013 /bin/sh

@jlliagre I am using freeBSD.
$ uname
FreeBSD
 
Old 04-09-2014, 11:17 AM   #13
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by anandg111 View Post
@jlliagre I am using freeBSD.
$ uname
FreeBSD
That explains a lot. FreeBSD /bin/sh is ash which doesn't support the brace syntax.

By the way, you are on the Linux Newbie Forum but FreeBSD is quite different from Linux distributions. There is a BSD specific forum here.
 
Old 04-09-2014, 03:12 PM   #14
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Quote:
Originally Posted by anandg111 View Post
@grail ->

$ echo $SHELL
/bin/sh
$ ls -l /bin/sh
-r-xr-xr-x 1 0 0 147056 Oct 1 2013 /bin/sh

@jlliagre I am using freeBSD.
$ uname
FreeBSD
As previously stated... read the man page... it tells you what is supported for file name expansion.
 
Old 04-09-2014, 03:48 PM   #15
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Moved: This thread is more suitable in *BSD forum and has been moved accordingly to help your thread/question get the exposure it deserves.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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



LinuxQuestions.org > Forums > Other *NIX Forums > *BSD

All times are GMT -5. The time now is 02:16 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