Announcement

Collapse
No announcement yet.

Using elif or else if?

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

    Using elif or else if?

    james147 was helping me with a bash script the other day. Today I was trying to modify this to search for archives with out launching a konsole(I ma using konsole with this script but only for the sake of debugging.) So the service menu is simply
    Exec=konsole --hold --workdir %f -e /home/xplorer4x4/.kde/share/kde4/services/ServiceMenus/searchforarchives.sh %U
    I will dump konsole --hold --workdir %f -e once the script is done. I need to run if find *.part01.rar else find *.rar and else fail. Here is what I came up with, but getting errors. Maybe I tried to jump in over my head here. Maybe I am just to tired. I was hopping you guys could point me in the right direction:
    Code:
    #!/bin/bash
    searchextractrar() {
        input="${1}"
    
        if [ find "${input}" -type f -name *.part01.rar ]; then
            kdialog --passivepopup "Extracting $FILE complete." 60
        elif [ find "${input}" -type f -name *.rar ]; then
            kdialog --passivepopup "Extracting $FILE complete." 60
        else
    	kdialog --passivepopup "Failed to extract $FILE." 60
          return 1  Change to exit 1 to stop the script on first failure
        fi
    }
    
    for file in ${@} ; do
        searchextractrar && unrar e $FILE
    done
    OS: Kubuntu 12.10/Windows 8
    CPU: Intel Core i7 2600K
    Motherboard: Gigabyte GA-Z77X-UD5H
    Memory: 2x4GB Corsair Dominator
    Graphics Card: MSI R7770
    Monitor: Dell 2208WFP
    Mouse: Mionix NAOS 5000
    PSU: Corsair 520HX
    Case: Thermaltake Mozart TX
    Cooling: Thermalright TRUE Black Ultra-120 eXtreme CPU Heatsink Rev C
    Hard Drives: 1x180 GB Intel 330 SSD - 1xWD 1 TB Caviar Black - 1xWD 2 TB Caviar Green - 2xWD 3 TB Caviar Green

    #2
    I worked on this some more:
    Code:
    searchextractrar() {
        input="${1}"
    
    
        if find "${input}" -type f -name *.part01.rar; then
            kdialog --passivepopup "Extracting $FILE complete." 60
        else if find "${input}" -type f -name *.rar; then
            kdialog --passivepopup "Extracting $FILE complete." 60
        else
    	kdialog --passivepopup "Failed to extract $FILE." 60
          return 1  Change to exit 1 to stop the script on first failure
        fi
      fi
    }
    
    for file in ${@} ; do
        searchextractrar && unrar e file
    done
    The only part that's not working is the passing of the search results to unrar as it comes back "Can not open file.rar."
    OS: Kubuntu 12.10/Windows 8
    CPU: Intel Core i7 2600K
    Motherboard: Gigabyte GA-Z77X-UD5H
    Memory: 2x4GB Corsair Dominator
    Graphics Card: MSI R7770
    Monitor: Dell 2208WFP
    Mouse: Mionix NAOS 5000
    PSU: Corsair 520HX
    Case: Thermaltake Mozart TX
    Cooling: Thermalright TRUE Black Ultra-120 eXtreme CPU Heatsink Rev C
    Hard Drives: 1x180 GB Intel 330 SSD - 1xWD 1 TB Caviar Black - 1xWD 2 TB Caviar Green - 2xWD 3 TB Caviar Green

    Comment


      #3
      bash uses elif as the else if statement, otherwise I think your are nesting if statements:
      Code:
      if ...
      else 
          if ...
          else ....
              if ...
              else...
              fi
          fi
      fi
      rather then
      Code:
      if ....
      elif ...
      elif ...
      elif ...
      else ...
      fi
      Code:
      for file in ${@} ; do    searchextractrar && unrar e file
      done
      searchextractrar has been passed no arguments, so ${1} is empty. Also, you need to use $ when accessing a varible:
      Code:
      for file in ${@} ; do    searchextractrar ${file} && unrar e ${file}
      done

      Comment


        #4
        James, thanks, I had already switched to else if in my script(see second post), but wasn't passing the search results to unrar as in your last bit of code. I switched file for ${file} and it worked. Actually I used ${file} ${input} so the extraction happened in the source dir instead og ~/Documents. The only minor annoyance is that I get the extraction completed dialog as soon as the extraction starts. I was able to overcome those, but the find command keeps pulling in non rar files and trying to unrar them.
        Code:
        #!/bin/bash
        searchextractrar() {
            input="${1}"
        
            if find . -type f -name '*.part01.rar' && unrar e ${file} ${input} ; then
                kdialog --passivepopup "Extracting ${file} complete." 60
            else if find . -type f -name '*.rar' && unrar e ${file} ${input} ; then
                kdialog --passivepopup "Extracting ${file} complete." 60
            else
        	kdialog --passivepopup "No archive found or failed to extract ${file}." 60
              return 1  Change to exit 1 to stop the script on first failure
              fi
           fi
        }
        
        for file in ${@} ; do
            searchextractrar ${file}
        done
        OS: Kubuntu 12.10/Windows 8
        CPU: Intel Core i7 2600K
        Motherboard: Gigabyte GA-Z77X-UD5H
        Memory: 2x4GB Corsair Dominator
        Graphics Card: MSI R7770
        Monitor: Dell 2208WFP
        Mouse: Mionix NAOS 5000
        PSU: Corsair 520HX
        Case: Thermaltake Mozart TX
        Cooling: Thermalright TRUE Black Ultra-120 eXtreme CPU Heatsink Rev C
        Hard Drives: 1x180 GB Intel 330 SSD - 1xWD 1 TB Caviar Black - 1xWD 2 TB Caviar Green - 2xWD 3 TB Caviar Green

        Comment


          #5
          Is there any reason you are not using ark's service menus to extract the files? I think it can combine split archives like that automatically.

          Comment


            #6
            Originally posted by james147 View Post
            Is there any reason you are not using ark's service menus to extract the files? I think it can combine split archives like that automatically.
            You mean the stock ark service menu? It cant search with in a folder, and if you search for .rar and pass it to ark, in theory. it would still try to extract each rar trying to overwrite the extracted file. You are right that it handles the part#.rar split archives properly by itself.

            Sent from my DROID2 Global
            OS: Kubuntu 12.10/Windows 8
            CPU: Intel Core i7 2600K
            Motherboard: Gigabyte GA-Z77X-UD5H
            Memory: 2x4GB Corsair Dominator
            Graphics Card: MSI R7770
            Monitor: Dell 2208WFP
            Mouse: Mionix NAOS 5000
            PSU: Corsair 520HX
            Case: Thermaltake Mozart TX
            Cooling: Thermalright TRUE Black Ultra-120 eXtreme CPU Heatsink Rev C
            Hard Drives: 1x180 GB Intel 330 SSD - 1xWD 1 TB Caviar Black - 1xWD 2 TB Caviar Green - 2xWD 3 TB Caviar Green

            Comment


              #7
              At the risk of getting too complicated, you might want to look into regular expressions. Here is a sample using Qt.
              Code:
              QRegExp rarExp("(rar|r\\d{1,3}$)");
              This works inside my newsreader for both rar formats.

              regular expressions and bash.
              FKA: tanderson

              Comment


                #8
                Code:
                find ${1} -type f -name '*.rar' -exec unrar e '{}' ';'
                is probally what you want, but note that fine is recursive... do you want it to find everything in a dirctory or everything below a directory?
                Last edited by james147; Oct 20, 2012, 02:01 PM.

                Comment


                  #9
                  Originally posted by blobfish View Post
                  At the risk of getting too complicated, you might want to look into regular expressions. Here is a sample using Qt.
                  Code:
                  QRegExp rarExp("(rar|r\\d{1,3}$)");
                  This works inside my newsreader for both rar formats.

                  regular expressions and bash.
                  RegEx makes me want to bash my head in for the most part! It would make the script cleaner for sure so I will look in to it.

                  Originally posted by james147 View Post
                  Code:
                  find ${1} -type f -name '*.rar' -exec unrar e '{}' ';'
                  is probably what you want, but note that fine is recursive... do you want it to find everything in a dirctory or everything below a directory?
                  I was using that directly in the exec lien of a service menu like this:
                  konsole --hold --workdir %f -e find -type f -name '*.rar' -exec unrar e '{}' ';'
                  However, when using -exec with in the script, it was throwing errors that it missed something for -exec so I was avoiding exec as my method technically works fine, but it would be nice to make it more efficient and only pass the search results to unrar rather then. Plus this method does not work well with archives using the part01.rar format rather then the rar and r01 method. When dealing with part01.rar type archives, it works in and endless loop that performs the extraction but then prompts you again to extract the archive once done. I think it is extract part01.rar and then trying to extract part02.rar and so on.
                  OS: Kubuntu 12.10/Windows 8
                  CPU: Intel Core i7 2600K
                  Motherboard: Gigabyte GA-Z77X-UD5H
                  Memory: 2x4GB Corsair Dominator
                  Graphics Card: MSI R7770
                  Monitor: Dell 2208WFP
                  Mouse: Mionix NAOS 5000
                  PSU: Corsair 520HX
                  Case: Thermaltake Mozart TX
                  Cooling: Thermalright TRUE Black Ultra-120 eXtreme CPU Heatsink Rev C
                  Hard Drives: 1x180 GB Intel 330 SSD - 1xWD 1 TB Caviar Black - 1xWD 2 TB Caviar Green - 2xWD 3 TB Caviar Green

                  Comment


                    #10
                    I just realized this script is a bit short coming. If I click a parent folder with multiple child folders containing split archives, it handles them fine. However if I try to choose multiple parent folders, the debug echo is not echoing any input and just launching console as if I had just started it from Lancelot Launcher. I also decided to pass the results to ark rather then unrar. This way I actually see a progress meeter during extraction.

                    Code:
                    #!/bin/bash
                    searchextractrar() {
                        input="${*}"
                        echo "${input}"
                    
                        if find . -type f -name '*.part01.rar' -exec ark --batch --autodestination --autosubfolder '{}' ';' ; then
                            kdialog --passivepopup "Extracting ${file} complete." 60
                        else if find . -type f -name '*.rar' -exec ark --batch --autodestination --autosubfolder '{}' ';' ; then
                            kdialog --passivepopup "Extracting ${file} complete." 60
                        else
                    	kdialog --passivepopup --icon error "No archive found or failed to extract ${file}." 60
                          return 1  Change to exit 1 to stop the script on first failure
                          fi
                       fi
                    }
                    
                    for file in ${@} ; do
                        searchextractrar ${file}
                    done
                    Also, I have read that -exec is very costly. IS there a better way then using exec?
                    Last edited by Xplorer4x4; Oct 21, 2012, 10:29 PM.
                    OS: Kubuntu 12.10/Windows 8
                    CPU: Intel Core i7 2600K
                    Motherboard: Gigabyte GA-Z77X-UD5H
                    Memory: 2x4GB Corsair Dominator
                    Graphics Card: MSI R7770
                    Monitor: Dell 2208WFP
                    Mouse: Mionix NAOS 5000
                    PSU: Corsair 520HX
                    Case: Thermaltake Mozart TX
                    Cooling: Thermalright TRUE Black Ultra-120 eXtreme CPU Heatsink Rev C
                    Hard Drives: 1x180 GB Intel 330 SSD - 1xWD 1 TB Caviar Black - 1xWD 2 TB Caviar Green - 2xWD 3 TB Caviar Green

                    Comment


                      #11
                      Originally posted by Xplorer4x4 View Post
                      Code:
                          input="${*}"
                      $1 is the first argument, $2 is the second $n is the nth argument, $# is the number of arguments and $@ is all the arguments not seen $* before ^^

                      exec '{}' ';' is costly as it executes a process for each file it finds, this isn't a problem if you don't have many files and the process isn't heavy. You might be better off with -exec '{}' '+' which builds a list of all the files and appends them to where the {} appear so will only execute the command onces and pass it a list of all the files found.

                      You might even want to use execdir:
                      -execdir command '{}' '+'
                      Like -exec, but the specified command is run from the subdirec-
                      tory containing the matched file, which is not normally the
                      directory in which you started find. This a much more secure
                      method for invoking commands, as it avoids race conditions dur-
                      ing resolution of the paths to the matched files. As with the
                      -exec option, the '+' form of -execdir will build a command line
                      to process more than one matched file, but any given invocation
                      of command will only list files that exist in the same subdirec-
                      tory. If you use this option, you must ensure that your $PATH
                      environment variable does not reference the current directory;
                      otherwise, an attacker can run any commands they like by leaving
                      an appropriately-named file in a directory in which you will run
                      -execdir.
                      Last edited by james147; Oct 22, 2012, 02:26 AM.

                      Comment


                        #12
                        From what I understand, ${1} and ${2} and so only capable of taking one input, but maybe I am misunderstanding why the echo returns a single value fine as long as there is only a single input/folder. Yet if there is multiple inputs/folders, it passes no inputs at all. As for $*, it was something I found on stackoverflow.

                        As for adding a + let me see if I understand this correct because that does sounds like a good thing..So if I put something like "konsole -e find whatever -exec unrar e {}" directly in the service menu, rather then directing to a bash script, and I select multiple folders, and it finds a single rar in each, then it it would spawn 2 instances of Konsole and perform both unrar commands at the same time. If I append '+' to the command, instead of spawning multiple konsole windows to unrar it would do unrar e file.rar and when that is done, then it will proceed to extract file2.rar?

                        I am also wondering if I am being redundant or breaking anything by using 2 semicolons like so:
                        Code:
                         ark --batch --autodestination --autosubfolder '{}' '+' ';' [B];[/B] then
                        As far as I can tell it does not hurt to use 2 semi colons as long as I place ticks on both sides when using '{}'
                        OS: Kubuntu 12.10/Windows 8
                        CPU: Intel Core i7 2600K
                        Motherboard: Gigabyte GA-Z77X-UD5H
                        Memory: 2x4GB Corsair Dominator
                        Graphics Card: MSI R7770
                        Monitor: Dell 2208WFP
                        Mouse: Mionix NAOS 5000
                        PSU: Corsair 520HX
                        Case: Thermaltake Mozart TX
                        Cooling: Thermalright TRUE Black Ultra-120 eXtreme CPU Heatsink Rev C
                        Hard Drives: 1x180 GB Intel 330 SSD - 1xWD 1 TB Caviar Black - 1xWD 2 TB Caviar Green - 2xWD 3 TB Caviar Green

                        Comment


                          #13
                          Info on $* http://stackoverflow.com/questions/8...-for-file-in-1

                          And I just saw you edit on + but it didn't rely clarify anything for me, but thanks for the info. It did a raise a concern in reference to sub directories though as I need the script to handle 2 situations.

                          Situation 1:
                          The input might be
                          /media/hdd/folder1withrars
                          or
                          /media/hdd/folder1withrars
                          /media/hdd/folder2withrars

                          Situation 2:
                          /media/hdd/folder/
                          -/folder1withrars/
                          -/folder2withrars/
                          OS: Kubuntu 12.10/Windows 8
                          CPU: Intel Core i7 2600K
                          Motherboard: Gigabyte GA-Z77X-UD5H
                          Memory: 2x4GB Corsair Dominator
                          Graphics Card: MSI R7770
                          Monitor: Dell 2208WFP
                          Mouse: Mionix NAOS 5000
                          PSU: Corsair 520HX
                          Case: Thermaltake Mozart TX
                          Cooling: Thermalright TRUE Black Ultra-120 eXtreme CPU Heatsink Rev C
                          Hard Drives: 1x180 GB Intel 330 SSD - 1xWD 1 TB Caviar Black - 1xWD 2 TB Caviar Green - 2xWD 3 TB Caviar Green

                          Comment


                            #14
                            Originally posted by Xplorer4x4 View Post
                            Code:
                             ark --batch --autodestination --autosubfolder '{}' '+' ';' [B];[/B] then
                            This should be:
                            Code:
                             ark --batch --autodestination --autosubfolder '{}' '+'[B];[/B] then
                            you either use ';' or '+'

                            and it will effectivly call "ark --batch --autodestination --autosubfolder archive1.rar archive2.rar archive3.rar" so one precess for all the archives rather then one for each.

                            You are also not using the $input anywhere in the function, you probally want "fine ${input} ..."
                            Last edited by james147; Oct 22, 2012, 04:06 AM.

                            Comment


                              #15
                              Ok thanks for the tip on using +.

                              As for find, I am using find ${input} but I was simply focusing on the ark part of it. It seems that something is wrong with the else if though. The script, it it's current form, will not search for .rar files, only part01.rar. I tried some echos to see what was being passed for {} and + but had no luck.
                              Code:
                              #!/bin/bash
                              searchextractrar() {
                                  input="${1}"
                                  echo "${1}"
                              
                                  if find ${input} -type f -name '*.part01.rar' -exec ark --batch --autodestination --autosubfolder '{}' '+' ; then
                                  echo "{}"
                                      kdialog --passivepopup "Extracting '+'complete." 60
                                  else if find ${input} -type f -name '*.rar' -exec ark --batch --autodestination --autosubfolder '{}' '+' ; then
                                  echo "{}"
                                      kdialog --passivepopup "Extracting '+' complete." 60
                                  else
                              	kdialog --passivepopup "No archive found or failed to extract ${file}." 60
                                    return 1  Change to exit 1 to stop the script on first failure
                                    fi
                                 fi
                              }
                              
                              for file in ${@} ; do
                                  searchextractrar ${file}
                              done
                              OS: Kubuntu 12.10/Windows 8
                              CPU: Intel Core i7 2600K
                              Motherboard: Gigabyte GA-Z77X-UD5H
                              Memory: 2x4GB Corsair Dominator
                              Graphics Card: MSI R7770
                              Monitor: Dell 2208WFP
                              Mouse: Mionix NAOS 5000
                              PSU: Corsair 520HX
                              Case: Thermaltake Mozart TX
                              Cooling: Thermalright TRUE Black Ultra-120 eXtreme CPU Heatsink Rev C
                              Hard Drives: 1x180 GB Intel 330 SSD - 1xWD 1 TB Caviar Black - 1xWD 2 TB Caviar Green - 2xWD 3 TB Caviar Green

                              Comment

                              Working...
                              X