-1

I am using 7za command to unzip password protected files. Lets consider a scenario, I have one file who's password is a combination of mmyy. dates(mmyy) can be for current month, previous month or previous to previous month. eg 1213 or 1113 or 1013 (mmyy:format) below is a sample code snippet i wrote

      function currmonth
     {
       curr_mon=`echo $(date +%x)`
       cyy=`echo $curr_mon| awk '{print substr($0,9,2)}'`
       cmm=`echo $curr_mon| awk '{print substr($0,1,2)}'`
           curr_pswd=`echo ${yy}${mm}`

     }
     function prevmonth 
     {
        prev_mon=`echo $(date +%x -d 'last month')`
        yy=`echo $prev_mon| awk '{print substr($0,9,2)}'`
        mm=`echo $prev_mon| awk '{print substr($0,1,2)}'`
        prev_pass=${yy}${mm}
     }
     function prev2month 
     {
        prev2_mon=`echo $(date +%x -d '2 month ago')`
        p2pyy=`echo $prev2_mon| awk '{print substr($0,9,2)}'`
        p2pmm=`echo $prev2_mon| awk '{print substr($0,1,2)}'`
        prev2mon_pass=${p2pyy}${p2pmm}

     }
     function IA_oper
     {

           files=`ls abc*.zip`
            for eachfile in $files; do

Not sure after this, here am not sure how do I loop them using If condition

        if [expression];then
      7za x -p$curr_pass $eachfile 


        elif [expression]; then
              7za x -p$prev_pass $eachfile 


       elif [expression]; then
      7za x -p$prev2mon_pass $eachfile 

       else
          mailx -s" cannot be extracted" abc@xyz.com
       fi
     done   
     }

I want to execute them in if loop if all the conditions fail it mails me with an error I am not sure how to do it please assist me with this

Rahul sawant
  • 415
  • 3
  • 6
  • 13
  • I have never heard of such a thing as an "if loop". – Robin Green Dec 04 '13 at 21:21
  • i am just trying a way in which i can loop the three possibilities together and see which one works if none i return an error mail – Rahul sawant Dec 04 '13 at 21:23
  • I don't think you understand what you are doing and I am voting to close this question. – Robin Green Dec 04 '13 at 21:24
  • @RobinGreen: i have one file which has a variable password,password is a combination of dates(mmyy), dates(mmyy) can be this month or previous month or 2 months back. i need to check which month works, for the same reason i tried looping them so i can determine which one works – Rahul sawant Dec 04 '13 at 22:06

1 Answers1

0

If you want to try them all, your "condition" needs to check for success or failure, which is reported via the exit status of a command. In bash, you find this with the variable $?

if [ $? -eq 0 ]; then
    echo "Success!"
fi

In your case, you want to stop trying new passwords once you find one that works, so use the keyword continue to jump back to the next iteration of the loop.

for eachfile in $files; do

    # Try each password, and move on as soon as we find one that works
    7za x -p$curr_pass $eachfile
    if [ $? -eq 0 ]; then
        continue;
    fi

    7za x -p$prev_pass $eachfile
    if [ $? -eq 0 ]; then
        continue;
    fi

    7za x -p$prev2mon_pass $eachfile
    if [ $? -eq 0 ]; then
        continue;
    fi

    # Fallthrough.  We get here only if all of the above failed.
    mailx -s" cannot be extracted" abc@xyz.com
done

By the way, you can save a bunch of awk parsing by allowing date to format your passwords for you. Just tell it you want MMYY:

date +%m%y -d '2 month ago'
Peter
  • 14,559
  • 35
  • 55
  • what if i want to rename the file for the password which works so in the each if block can i add an else which will help me to rename the file if the password works? – Rahul sawant Dec 05 '13 at 21:05
  • If a password works, the continue inside the `if` clause is hit, which will take execution back to the next "eachfile" in the `for` loop. So if you want to do something additional on success, put it before the `continue`. As this script gets more complex, consider putting your possible passwords in an array so you can loop through them and remove the 3x duplicate code. – Peter Dec 05 '13 at 21:57
  • I got it Thanks Peter. yes you are right an array would make it more efficient. – Rahul sawant Dec 05 '13 at 22:56