Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

The "Setup" script will be used multiple times to create as many different scripts as are required.

  • There are up to 6 variables I need to collect.

  • The Collection of variables needs to be limited to however long the
    directory is,
    (i.e. 3 required for ~/AOKP/vendor/aokp and 2 required for ~/AOKP/Build)

Expected Output: a child script named using the correct number of variables (ie frameworks_base.sh)

The "Setup" script follows:

#Create Array
echo "Please enter your Working Directory ROOT (Just the word: For ~/OurROM/ type: OurROM)"
read root
echo "Please Enter the Subequent Directories to the location of your git directory
# -a makes read command to read into an array
read -a gitdir
# get number of elements in the array
elements=${#gitdir[@]}
index=0

while [ "$index" -lt "$elements" ]
do 
?????????
echo "Your Directory is ~/$root/${gitdir[0]}/${gitdir[1]}/${gitdir[2]}" 

The Child Script (NOT needed to answer... Informational only) that needs the changes follows:

            #!/bin/bash

    # This script can be modified to allow for any directory
    # Be sure to change all Directory References. References will be proceded in the line above by a *

    clear

           # * Change if Required
    cd ~/OurROM/.scripts
    wait

    while true; do
    clear
                                   # * Change if Required
    echo "What would you like to do with .scripts?"
    echo "1.  Enter Commit Message"
    echo "2.  Add All Changes"
    echo "3.  Commit All Changes"
    echo "4.  Push All Changes"
    echo "5.  Reset All Changes"
    echo "6.  Merge Current Directory"
    echo "7.  REVERT a Commit"
    echo "8.  "
    echo "9.  "
    echo "10. "
    echo ""
    echo "Current Commit Message: $commit"
    echo ""
    echo -n "Enter your choice, or 0 for exit: "
    read choice
    echo

    case $choice in
         1)
         clear
         echo "Please Enter Your Commit Message:"
         read commit
         echo ""
         echo "Commit: '$commit' - has been recorded"
         echo ""
         read -p "Press [Enter] key to continue..."
         ;;
         2)
         clear
         echo "Deleting all Hidden files"
         find ~/OurROM/ -iname "*.*~" -type f -exec rm {} \;
         wait
         echo "All HIDDEN files Deleted"
         git add --all && git add . && git add -u
         wait
         echo ""
         echo "Changes have been added"
         read -p "Press [Enter] key to continue..."
         ;;
         3)
         clear
         echo "Executing Commit..."
         git commit -m "$commit"
         wait
         echo ""
         echo "Message Commited"
         echo ""
         read -p "Press [Enter] key to continue..."
         ;;
         4)
         clear
         echo "Pushing your Commit..."
                                        # * Change if Required
         git push [email protected]:OurROM/.scripts.git HEAD:jb-mr1
         wait
         echo ""
         echo "$commit - has been pushed to OurROM"
         echo ""
         read -p "Press [Enter] key to continue..."
         ;;
         5)
         clear
         git reset --hard HEAD && git clean -f && git checkout origin/jb-mr1
         wait
         echo ""
         echo ".scripts has been RESET"
         echo ""
         read -p "Press [Enter] key to continue..."
         ;;
         6)
         clear
         git merge origin/jb-mr1
         wait
         echo ""
         echo "Local Directory is Merged with Online Data"
         echo "" 
         read -p "Press [Enter] key to continue..."
         ;;
         7)
         clear
         echo ""
         echo "Paste the Commit Number you would like to Revert:"
         read revert 
         echo "Commit #: '$revert' - will be reverted. Is this Correct?"
            select yn in "Yes" "No"; do
                case $yn in
                    Yes ) git revert $revert; wait; echo "Commit $revert has been reverted"; read -p "Press [Enter] key to continue..."; break;;
                    No ) break;
                esac
             done
         echo ""
         ;;
         8)
XXXXX REMOVED FOR BREVITY XXXXXXX
         *)
         echo "That is not a valid choice, try a number from 0 to 10."
         ;;
    esac  
    done

PART 3: I've Used All the Input from You and gotten what I want, now for some finesse! How Do I appropriately nest the following 2 While statements to Gather the Data in one swoop? The Array will be the same for both only the root may change So I have 2 separate menu Items. I'd like to make it one but my nesting fails...

         8)
     clear
     echo ""
     #Create Array
     echo "Please enter your Working Directory ROOT (Just the word: For ~/OurROM/ type: OurROM)"
     read root
     echo "Please Enter the Subequent Directories to the location of your git directory"
     echo "Again, just the words: For ~/OurROM/frameworks/base type: frameworks base"
     # -a makes read command to read into an array
     read -a gitdir
     # get number of elements in the array
     elements=${#gitdir[@]}
     fullPath="~/${root}"
     index=0

while [ "$index" -lt "$elements" ] ; do
         # append values from $gitdir until you are done
    fullPath="${fullPath}/${gitdir[$index]}"
    (( index++ ))
done 
     ;;
     9)
     clear
     echo ""
     #Create Array
     echo "Please enter your remote github.com repository ROOT (Just the word: For https://github.com/OurROM/ type: OurROM)"
     read repo
     echo "Please Enter the Subequent Directories to the location of your git repository"
     echo "Again, just the words: For https://github.com/OurROM/frameworks/base type: frameworks base"
     # -a makes read command to read into an array
     read -a repodir
     # get number of elements in the array
     elements=${#repodir[@]}
     repoSave="${repo}/"
     index=0

while [ "$index" -lt "$elements" ] ; do
         # append values from $gitdir until you are done
    repoSave="${repoSave}${repodir[$index]}_"
    (( index++ ))
    repoPath=${repoSave%?}
done 
     ;;

I've tried to nest like this but it Truncates the First output to 1 array value:

     8)
     clear
     echo ""
     #Create Array
     echo "Please enter your Working Directory ROOT (Just the word: For ~/OurROM/ type: OurROM)"
     read root
     echo "Please enter your remote github.com repository ROOT (Just the word: For https://github.com/OurROM/ type: OurROM)"
     read repo     
     echo "Please Enter the Subequent Directories to the location of your git directory"
     echo "Again, just the words: For ~/OurROM/frameworks/base type: frameworks base"
     # -a makes read command to read into an array
     read -a gitdir
     # get number of elements in the array
     elements=${#gitdir[@]}
     fullPath="~/${root}"
     repoSave="${repo}/"     
     index=0

while [ "$index" -lt "$elements" ] ; do
         # append values from $gitdir until you are done
    fullPath="${fullPath}/${gitdir[$index]}"
    (( index++ ))

    index=0
    while [ "$index" -lt "$elements" ] ; do
        repoSave="${repoSave}${gitdir[$index]}_"
        (( index++ ))
        repoPath=${repoSave%?}
    done
done 
     ;;

Where did I go Wrong?

share|improve this question
    
+1 for lots of code and chutzpah, -1 for rambling presentation, net = 0. Boil your problem down to 2-5 lines of code, 'If I can do this, then I'll be able to do a big chunk more' (maybe not everything, but then you can ask another question). Good luck. –  shellter Jul 19 '13 at 21:21
    
chutzpah! I love it. (For all others: cajones, gumption, etc). OK I did what you asked.. although it gave me adjidah. –  TheByteSmasher Jul 19 '13 at 21:34
    
Still too much ;-). Really! Don't make us understand this big context, C'mon, anyone that can write that much code can the fall back to an information gather mode and ask 'How do I do X' and then be able to work with that to enhance your main project. I'm leaving office now. wont be able to respond for a while. Good luck. –  shellter Jul 19 '13 at 21:39
1  
@TheByteSmasher: use select bash builtin to create a nice menu, instead of a lot of echo and alike. –  TrueY Jul 19 '13 at 22:37
    
+1 for taking feedback. Now we (I) can understand what you need. Sorry, now busy at home. @TrueY is onto something with select. Good luck. –  shellter Jul 19 '13 at 23:33
show 1 more comment

1 Answer

up vote 0 down vote accepted

This answer, a work in progress; Your code

while [ "$index" -lt "$elements" ]
do 
 ?????????
 echo "Your Directory is ~/$root/${gitdir[0]}/${gitdir[1]}/${colours[2]}" 
done

becomes

fullPath="/${root}/"
index=1
while [ "$index" -lt "$elements" ] ; do 
    # append values from $gitdir until you are done
    fullPath="${fullPath}/${gitdir[$index]}"
    (( index++ ))
done 
# not sure how colours got introduced to this but same idea  
fullPath="${fullPath}/${colours[2]}"

echo "Your Directory is ~/${fullPath}" 

use of (( index++ )) implies using a version of bash, ksh, zsh (maybe others) that support arithmetic evaluations.


That said, it's not clear what your input into gitdir[@] will be and why you need to "count" the levels. Why not just accept user input as arguments, document the order of arguments and then enforce that by testing inputs before starting actual processing. Something like ...

 case $# in 
    [012] ) echo "usage: myScript root path colour [colour2 ...]" 1>&2 ; exit 1 ;;
    3 )
      # minimal number of args acceptable
      root="$1"
      localPath="$2"
      colour[1]="$3"
    ;;
    * )
      # assume more than 1 colour passed
      root="$1"
      localPath="$2"
      shift 2 ; cntr=1
      while (( $# > 1 )) ; do
        colour[$cntr]="$1"
        shift
        ((cntr++))
      done
   ;;
   esac

   # you get the idea. recall that a leading '/' on localPath will mess things up
   # build a trap for that like
   case "${localPath}" in
       [/]* ) echo "bad input for localPath, no leading '/' char please" 1>&2 ;;
   esac

   # validate inputs
   if [[ ! -d "${root}" ]] ; then
      echo "can't find rootdir=${root}" 1>&2
      cleanInput=false
   fi
   if ! cd "${root}" ; then
      echo "can't find rootdir=${root}" 1>&2
      exit 1
   fi

   # we are in root now, make sure that localPath is real
   if [[ ! -d "${localPath}" ]] ; then
      echo "can't find localPath=${localPath}" 1>&2
      cleanInput=false
   fi

   if ! ${cleanInput:-true} ; then
      echo "found errors, please try again"
      exit 1
   fi

   # now do your real stuff.
   echo "about to use args passed in root=${root}, localPath=${localPath} colours=${colours[@]} ...."

These are meant to be illustrative about various techniques you can use and NOT a perfect solution to your problem (as I'm still unclear if you have a very specific need or if you're just learning about various ways to solve your problem). Now you have a tool kit of several techniques that should help you organize and test your user input before starting the main processing of your script.

Also per other comments of others, you may find the shell select "framework" valuable, so search here for [bash] select. I've seen numerous good solutions that use that for offering users a list of possible actions.

IHTH.

share|improve this answer
    
Oh! colours was a derp!!! should be index. It came from code I was using to test technique. That being said: I need to capture a finite amount of root + subdirectories. I'm creating this so that 7+ users can have custom scripts to aid in Git functions (those scripts I've written but they are static and only work with ONE directory and/or git repository per script). So sometimes I'll need to use the captured information to produce a dir (~/OurROM/frameworks/base) & sometimes to refer to a Github URL (OurROM/frameworks_base) but the root+subs are of varying lengths depending on the one in use. –  TheByteSmasher Jul 20 '13 at 16:01
    
So, I don't know how how many arguments will be passed... that's one issue, the other is that they will be outputted in different manners. if you notice in my child scrip example the #* Change if required comments are the elements I'm trying to make dynamic so each outputted script will be unique, based on the "setup" script you are wonderfully helping me with... –  TheByteSmasher Jul 20 '13 at 16:10
    
ok, so take colours out of my code.Do you understand that $# tells how many arguments are on the cmd line, and that after you assign something like root=$1 ; colours=$2; shift 2 that now the value of $# is reduced by 2? (shift 2, get it? ;-)? (if not, take the time to build a small test case and experiment). What is left is a variable list of elements that can be "glued together" as localPath, using localPath="${localPath}/$1" in a loop and shifting away the consumed argument, that reduces the value of $#. When the value of $# is 0, there are no more arguments. Good luck and IHTH. –  shellter Jul 20 '13 at 18:27
    
Thank you very much. I understand what you've said, now I need to do as suggested and test it out a bit to... uh... understand it in my terms.. :) you are a talented teacher... hopefully I'll be able to pass this on in the future. –  TheByteSmasher Jul 21 '13 at 9:30
    
I'm glad that helped. I just noticed that I missed incrementing cntr in my 2nd set of code. I've added that as ((cntr++)). If you look thru the normal range of questions tagged bash, note that many have multiple answers AND more importantly, note that many are more focused to asking 1 or 2 things; 'I tried this, why isn't it working', is really the form for questions on StackOverflow. Keep posting (reduced questions;-), and ... Good luck. –  shellter Jul 21 '13 at 19:03
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.