Find the answer to your Linux question:
Results 1 to 6 of 6
I'll make up some (probably ugly) alternative just to have the script working, but I'd like to ask how it could be done more directly anyway. If that's actually possible, ...
  1. #1
    Linux Newbie
    Join Date
    Apr 2007
    Posts
    211

    Mostly curiosity: how to make "mkdir {1..$number}" work from a script?

    I'll make up some (probably ugly) alternative just to have the script working, but I'd like to ask how it could be done more directly anyway. If that's actually possible, which I'm starting to suspect that is not the case.


    What I've tried so far:


    mkdir {1..$numdirs}

    mkdir {1.."$numdirs"}

    mkdir "{1.."$numdirs"}"

    mkdir '{1..'$numdirs'}'

    mkdir \{1..$numdirs\}

    mkdir \{1.."$numdirs"\}

    mkdir {1..$(echo $numdirs)}

    mkdir {1..$(echo ${numdirs})}

    mkdir {1..${numdirs}}

    trick="{1..$numdirs}"
    mkdir $trick


    The same result for all, folders literally called "{1..5}" or whatever is the number assigned to the variable.

  2. #2
    Just Joined!
    Join Date
    Aug 2005
    Posts
    65
    im sorry i can't understand ur problem but if u need to make the folder literally like {1..$number} u can try this
    Code:
    mkdir  {1..\$number}
    if u need the name to be literally something like {1..$variablenumber} u can try this
    Code:
    {1..\$$number}

  3. #3
    drl
    drl is online now
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    Here are two methods, one with bash, one with zsh:
    Code:
    #!/bin/bash
    
    # @(#) s6	Demonstrate sequence expression.
    
    echo
    echo " This version of bash is:"
    bash --version
    echo
    
    lo=1
    hi=10
    
    echo {$lo..$hi}
    
    echo
    eval echo -n {$lo..$hi}
    echo
    
    echo
    t1=$( eval echo -n {$lo..$hi} )
    echo mkdir $t1
    
    echo
    echo " This version of zsh is:"
    zsh --version
    zsh -c "echo mkdir {$lo..$hi}"
    
    exit 0
    producing:
    Code:
    % ./s6
    
     This version of bash is:
    GNU bash, version 3.2.39(1)-release (x86_64-pc-linux-gnu)
    Copyright (C) 2007 Free Software Foundation, Inc.
    
    {1..10}
    
    1 2 3 4 5 6 7 8 9 10
    
    mkdir 1 2 3 4 5 6 7 8 9 10
    
     This version of zsh is:
    zsh 4.3.6 (x86_64-unknown-linux-gnu)
    mkdir 1 2 3 4 5 6 7 8 9 10
    See man pages for details ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  4. #4
    Linux Newbie
    Join Date
    Apr 2007
    Posts
    211
    deepinlife,

    What something like "mkdir {1..5}" does in the prompt line is to make five folders, named 1, 2, 3, 4 and 5. However, from a script, these numbers can't be set by variables in the most obvious way(s), as I tried, as shown in the first post. All those different ways will just make one folder called "{1..5}", if the variables are 1 and 5.








    dlr, thanks!

    I was thinking that there would be something really simple that would make me feel really stupid with the "solution" that would be more a matter of use quotes, parenthesis or slashes in a correct way, but this still look more correct somehow than what I've managed to do:

    hi=$numdirs
    until ((hi == 0)) ; do
    mkdir $hi
    hi=$(( hi-1 ))
    done



    Side by side it isn't much larger, but now I have this "eval" thing to read about and eventually use in other circumstances. Not to mention that the loop looks stupid.

    It's funny how sometimes there are more than one way to do the same thing, some which may look radically different. I used to set a random wallpaper with these lines:

    Background=$(find ${subdir[$((n))]} -iname '*')
    background=($Background) # Read into array variable.
    num_background=${#background[*]} # Count how many elements.
    a=$RANDOM%num_background
    Esetroot -fit "${background[$((a))]}"

    Not to mention the confusion with Background and background, which is just a minor point, compare with that:

    find $imagesroot -type f -name '*.png' -o -name '*.jpg' -o -name '*.gif' -follow >$maindir/wplist.temp
    wp=$(shuf -n 1 $maindir/wplist.temp)
    Esetroot -fit "$wp"



    Not only it looks crisply clear, but for some reason now it will accept filenames with spaces, instead of trying to set the first word before a space as the wallpaper. Having the list to sort from as a text file also make it a bit faster than "find" it every time.

  5. #5
    drl
    drl is online now
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    The behavior of this command is a result of the sequence of steps in processing the command. In bash, the brace expansion is done before variable (parameter) expansion. So for constants inside braces, you get what you might expect. However, for variables, you don't. See Shell Expansions - Bash Reference Manual for the exact sequence.

    Other shells have different sequences. The shells ksh and zsh behave (in this case) the way you may expect, but other results might be unexpected because of that.

    I used bash in this instance because it's the most common and you didn't specify which shell you were using. Other shells may be more attractive, but possibly not always available. Caveat {emptor,lector,utilitor,scriptor} (with help from Wapedia - Wiki: List of Latin phrases: C).

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  6. #6
    Linux Newbie
    Join Date
    Apr 2007
    Posts
    211
    Thanks. I was using bash indeed. I wonder how you guys manage to keep up with so many different sintaxes. I keep ranting that everything should be just like turbo basic or even msx basic.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...