BASH:如何创建安装脚本以构建另一个脚本

时间:2013-07-19 19:36:54

标签: arrays linux bash variables choice

将多次使用“Setup”脚本来创建所需数量的不同脚本。

  • 我需要收集最多6个变量。

  • 变量的集合需要限制在很长的时间内 目录是,(即〜/ AOKP / vendor / aokp需要3个,需要2个 〜/ AOKP / Build)

预期输出:使用正确数量的变量命名的子脚本(即frameworks_base.sh)

“安装”脚本如下:

#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]}" 

需要更改的子脚本(不需要回答......仅限信息)如下:

            #!/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 git@github.com: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

第3部分:我已经使用了你的所有输入并得到了我想要的东西,现在有些技巧!如何适当地嵌套以下2个While语句来一次收集数据?数组将是相同的只有根可能会改变所以我有2个单独的菜单项。我想把它做成一个,但我的筑巢失败......

         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 
     ;;

我试图像这样嵌套,但它将First输出截断为1个数组值:

     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 
     ;;

我去哪了错?

1 个答案:

答案 0 :(得分:0)

这个答案,正在进行中;你的代码

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

变为

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}" 

使用(( index++ ))意味着使用支持算术评估的bash,ksh,zsh(可能是其他版本)。


那就是说,你不清楚你对gitdir [@]的输入是什么以及为什么你需要“计算”这些级别。为什么不直接接受用户输入作为参数,记录参数的顺序,然后通过在开始实际处理之前测试输入来强制执行。有点像...

 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[@]} ...."

这些是关于你可以使用的各种技术的说明,而不是你问题的完美解决方案(因为我仍然不清楚你是否有非常具体的需求或者你是否只是了解解决问题的各种方法)。现在,您有一个包含多种技术的工具包,可以帮助您在开始主要处理脚本之前组织和测试用户输入。

另外,对于其他人的评论,您可能会发现shell select“框架”很有价值,因此请在此处搜索[bash] select。我已经看到了很多很好的解决方案,用于为用户提供可能的操作列表。

IHTH。