Announcement

Collapse
No announcement yet.

Can't use braces {} with 'mkdir -p

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Can't use braces {} with 'mkdir -p

    I can't figure out this.

    I have a small bash script like this one;

    #!/bin/sh
    (...)
    mkdir -p ~/test/{aa,bb,cc,dd}
    (...)
    It should create a folder called "test" in home directory and inside this folder it should exist 4 folders named aa bb cc dd.

    BUT NO,

    It creates the folder "test" and inside this one it creates one single folder with name {aa,bb,cc,dd}!!!!!!!

    If i run the command "mkdir -p ~/test/{aa,bb,cc,dd}" directly from Konsole it's OK, but inside the script no.

    What's the problem with braces {} inside script??

    I've also tried with bash instead of sh (#!/bin/bash), but the problem is the same.


    Any ideas please


    Thanks in advance.

    #2
    Re: Can't use braces {} with 'mkdir -p

    Umm ... seems to work as expected for me. Am I doing it different?

    Code:
    $ mkdir -vp ~/test/{aa,bb,cc}
    mkdir: created directory `/home/ken/test'
    mkdir: created directory `/home/ken/test/aa'
    mkdir: created directory `/home/ken/test/bb'
    mkdir: created directory `/home/ken/test/cc'
    I'm using 8.04 Hardy. What version of Ubuntu are you using and what version of mkdir? What filesystem are you working in?

    Code:
    $ mkdir --version
    mkdir (GNU coreutils) 6.10
    Copyright (C) 2008 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <[url]http://gnu.org/licenses/gpl.html>[/url]
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.
    
    Written by David MacKenzie.
    $ lsb_release -a
    No LSB modules are available.
    Distributor ID: Ubuntu
    Description:  Ubuntu 8.04.3 LTS
    Release:    8.04
    Codename:    hardy
    $ df -hT ~/
    Filesystem  Type  Size Used Avail Use% Mounted on
    /dev/sda3   ext3  364G 152G 194G 44% /
    Edit:Might also be helpful to know the BASH version.

    Code:
    $ bash --version
    GNU bash, version 3.2.39(1)-release (i486-pc-linux-gnu)
    Copyright (C) 2007 Free Software Foundation, Inc.
    Just in case it matters that it is in a script, I wrote the following script and saved with the file name script.
    Code:
    #!/bin/bash
    mkdir -p ~/test/{aa,bb,cc,dd}
    The results are the same obtained from the immediate mode command line.
    Code:
    $ ./script
    $ ls -R test/
    test/:
    aa bb cc dd
    
    test/aa:
    
    test/bb:
    
    test/cc:
    
    test/dd:
    By the way, this thread might be more appropriate in the Coding/Scripting forum.
    Welcome newbies!
    Verify the ISO
    Kubuntu's documentation

    Comment


      #3
      Re: Can't use braces {} with 'mkdir -p

      I agree

      Comment


        #4
        Re: Can't use braces {} with 'mkdir -p

        Originally posted by luispt
        I've also tried with bash instead of sh (#!/bin/bash), but the problem is the same.
        #!/bin/bash really should work, but #!/bin/sh won't. The default 'sh' shell in buntus is dash, which AFAIK won't behave as expected with your command.

        1. Make sure you use #!/bin/bash (remember to save and run the correct script)
        2. Also make sure you don't use quotes around the command.
        3. If none of these work, post the entire script so we might have a go with it.

        Comment


          #5
          Re: Can't use braces {} with 'mkdir -p

          This is a well known problem in *buntu. See the discussion here (and its status, in the minds of the devs). It is (in my opinion) the very worst example of "I know better than you" ism in the entire distribution. The concept that saving a few seconds at boot time is worth screwing up scripts for everyone, forever is appalling to me.

          The solution is simple. In konsole or yakuake:
          cd /bin
          sudo mv sh sh.old
          sudo ln -T bash sh
          In the event that I have screwed up my script (a high probability because I can't type), all you have to do is a simple re-rename.

          Comment


            #6
            Re: Can't use braces {} with 'mkdir -p

            Granted, dash may have its troubles, but:

            Originally posted by luispt
            I've also tried with bash instead of sh (#!/bin/bash), but the problem is the same.

            How can this be? My Bash script definitely works as expected.
            Welcome newbies!
            Verify the ISO
            Kubuntu's documentation

            Comment


              #7
              Re: Can't use braces {} with 'mkdir -p

              Originally posted by askrieger
              This is a well known problem in *buntu. See the discussion here (and its status, in the minds of the devs). It is (in my opinion) the very worst example of "I know better than you" ism in the entire distribution. The concept that saving a few seconds at boot time is worth screwing up scripts for everyone, forever is appalling to me.

              The solution is simple. In konsole or yakuake:
              cd /bin
              sudo mv sh sh.old
              sudo ln -T bash sh
              In the event that I have screwed up my script (a high probability because I can't type), all you have to do is a simple re-rename.
              Thanks askrieger, now my script works. This workaround solved the problem.

              To Telegard: for sure I've tried #!/bin/bash in the script, but it definitely it didn't worked.

              Regards and thanks again for your help. It saved my work/script.


              Comment


                #8
                Re: Can't use braces {} with 'mkdir -p

                Originally posted by askrieger
                The solution is simple. In konsole or yakuake:
                cd /bin
                sudo mv sh sh.old
                sudo ln -T bash sh
                You can also do 'sudo dpkg-reconfigure dash', and choose your preference when asked whether dash should be the sh shell. (Technically, though, all scripts that use bashism should use #!/bin/bash rather than #!/bin/sh, even though changing the default sh to bash will work around that)

                Originally posted by luispt
                for sure I've tried #!/bin/bash in the script, but it definitely it didn't worked.
                That is very strange, how were you running the script? (Even though your current problem is solved, this may come to haunt you later...it should have worked)

                Comment


                  #9
                  Re: Can't use braces {} with 'mkdir -p

                  The main reason for changing "sh" from dash to bash is that some time in the distant future, someone (with a life) might have forgotten that *buntu sets "sh" to dash. They might then COPY a script from another source, e.g. the web or a magazine, that uses "#!/bin/sh" instead of the easy to remember "hash-bang-slash-bin-slash-bash", where hash = '#' and slash = '/'. If the computer in question had been rebooted into Kubuntu once a day for a couple of years, by someone who was obliged to use windoze, the user might have saved an hour and a half over that time. Trying to figure out why ONE script crashes will sop up all the time saved.

                  I leave it to the reader to determine whether the user would have even noticed the trivial reduction in the duration of the boot process. They would certainly notice the time lost in debugging the unusable script. But, I am not a developer.

                  Comment


                    #10
                    Re: Can't use braces {} with 'mkdir -p

                    Originally posted by kubicle
                    Technically, though, all scripts that use bashism should use #!/bin/bash rather than #!/bin/sh . . .
                    +1

                    Shebang_(Unix)
                    The '#!' magic - details about the shebang mechanism on various Unix flavours
                    bash vs sh

                    Originally posted by luispt
                    for sure I've tried #!/bin/bash in the script, but it definitely it didn't worked.
                    That is very strange, how were you running the script? (Even though your current problem is solved, this may come to haunt you later...it should have worked)
                    Indeed, this is quite troublesome. That is why I requested OP post version information on his system. We need to know more about the unique environment of his system to determine the cause of the failure. Bash works fine for me, and every one else AFAIK.
                    Welcome newbies!
                    Verify the ISO
                    Kubuntu's documentation

                    Comment


                      #11
                      Re: Can't use braces {} with 'mkdir -p

                      Here is the version's of mkdir, release and bash:


                      luis@ubuntu:~/minitux$ mkdir --version
                      mkdir (GNU coreutils) 7.4
                      Copyright (C) 2009 Free Software Foundation, Inc.
                      Licença GPLv3+: GNU GPL versão 3 ou posterior <http://gnu.org/licenses/gpl.html>.
                      Isto é software grátis: você é livre para modificá-lo e distribuí-lo.
                      Não tem GARANTIA, até ao limite garantido por lei.

                      Escrito por David MacKenzie.




                      luis@ubuntu:~/minitux$ lsb_release -a
                      No LSB modules are available.
                      Distributor ID: Ubuntu
                      Description: Ubuntu 9.10
                      Release: 9.10
                      Codename: karmic




                      luis@ubuntu:~/minitux$ bash --version
                      GNU bash, version 4.0.33(1)-release (i486-pc-linux-gnu)
                      Copyright (C) 2009 Free Software Foundation, Inc.
                      License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

                      This is free software; you are free to change and redistribute it.
                      There is NO WARRANTY, to the extent permitted by law.

                      Comment


                        #12
                        Re: Can't use braces {} with 'mkdir -p

                        Since you are on 9.10 Karmic should I assume your file system is EXT4?

                        Code:
                        df -hT ~/
                        Filesystem  Type  Size Used Avail Use% Mounted on
                        /dev/sda1   ext4  7.5G 2.5G 4.7G 36% /     
                        $ mkdir --version
                        mkdir (GNU coreutils) 7.4
                        Copyright (C) 2009 Free Software Foundation, Inc.
                        License GPLv3+: GNU GPL version 3 or later <[url]http://gnu.org/licenses/gpl.html>[/url].
                        This is free software: you are free to change and redistribute it.
                        There is NO WARRANTY, to the extent permitted by law.
                        
                        Written by David MacKenzie.
                        $ lsb_release -a
                        No LSB modules are available.
                        Distributor ID: Ubuntu
                        Description:  Ubuntu 9.10
                        Release:    9.10
                        Codename:    karmic
                        $ bash --version
                        GNU bash, version 4.0.33(1)-release (i486-pc-linux-gnu)
                        Copyright (C) 2009 Free Software Foundation, Inc.
                        License GPLv3+: GNU GPL version 3 or later <[url]http://gnu.org/licenses/gpl.html>[/url]
                        
                        This is free software; you are free to change and redistribute it.
                        There is NO WARRANTY, to the extent permitted by law.
                        $ echo '#!/bin/bash
                        > mkdir -p ~/test/{aa,bb,cc,dd}' > script
                        $ chmod +x script
                        $ ./script
                        $ ls -R test/
                        test/:
                        aa bb cc dd
                        
                        test/aa:
                        
                        test/bb:
                        
                        test/cc:
                        
                        test/dd:
                        All my version information seems to be the same as yours, and it definitely works here for me. Something strange is going on here!
                        Welcome newbies!
                        Verify the ISO
                        Kubuntu's documentation

                        Comment


                          #13
                          Re: Can't use braces {} with 'mkdir -p


                          Originally posted by luispt
                          for sure I've tried #!/bin/bash in the script, but it definitely it didn't worked.
                          Just a thought, were you running the script with something like 'sh yourscript' instead of just 'yourscript'?

                          Comment

                          Working...
                          X