LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   *BSD (https://www.linuxquestions.org/questions/%2Absd-17/)
-   -   Use of {} (https://www.linuxquestions.org/questions/%2Absd-17/use-of-%7B%7D-4175500855/)

anandg111 04-07-2014 09:21 AM

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.

Soadyheid 04-07-2014 09:33 AM

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!

:hattip:

sag47 04-07-2014 09:42 AM

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.

anandg111 04-07-2014 09:54 AM

set +o noglob
 
I tried `set +o noglob` and the other solution you suggested but still getting the same result.

suicidaleggroll 04-07-2014 10:06 AM

What shell are you using?

anandg111 04-07-2014 10:08 AM

bourne shell
 
bourne shell

grail 04-07-2014 10:27 AM

So your output is as follows:
Code:

$ echo $SHELL
/bin/sh


anandg111 04-07-2014 11:42 AM

yes

grail 04-08-2014 02:04 AM

In that case, please perform the following and provide the output:
Code:

ls -l /bin/sh

sag47 04-08-2014 08:41 AM

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.

jlliagre 04-08-2014 09:00 AM

Quote:

Originally Posted by anandg111 (Post 5148184)
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.

anandg111 04-09-2014 07:53 AM

@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

jlliagre 04-09-2014 11:17 AM

Quote:

Originally Posted by anandg111 (Post 5149390)
@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.

sag47 04-09-2014 03:12 PM

Quote:

Originally Posted by anandg111 (Post 5149390)
@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.

colucix 04-09-2014 03:48 PM

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


All times are GMT -5. The time now is 06:36 PM.