制作一个 Git Bash“Shell”脚本来执行已经存在的命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10272199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:06:46  来源:igfitidea点击:

Making a Git Bash "Shell" Script to execute already existing commands

gitbashcommandexecute

提问by user1344948

i would like to make a "script" to perform some commands for me in git bash. i would then start git bash and type git myScriptName and hit enter, the it would perform:

我想制作一个“脚本”来在 git bash 中为我执行一些命令。然后我将启动 git bash 并输入 git myScriptName 并按回车键,它将执行:

    cd myProjectFolderName (ENTER)
    git init (ENTER)
    git add -A (ENTER)
    git commit -m 'letMeWriteSomeThingHereAndIHitEnterAndItEndsCommentWith' (ENTER)
    git push myRemoteName myBranch (ENTER)

and then do nothing, i would also like to do the same with:

然后什么都不做,我也想这样做:

    cd myProjectFolderName (ENTER)
    git init (ENTER)
    git pull myRemoteName myBranch (ENTER)

and then do nothing.

然后什么都不做。

Thanks a bunch for any help regarding this, a plus would be if someone even went ahead and made the script :) Thanks

非常感谢您对此的任何帮助,如果有人甚至继续制作脚本,那就更好了:)谢谢

回答by user1251007

You could start with this:

你可以从这个开始:

gitCommands.sh:

gitCommands.sh

function go_on {
  echo -ne " [Y, n]\r"
  read answer
  if [ "$answer" = "n" ]; then
    echo "exit"
    exit 0
  fi
}

function call {
  go_on ""
  
  echo ""
}

call "cd myProjectFolderName"

echo "Type commit message"
read commit_message
call "git commit -m $commit_message"

The function callexecutes the function 'go_on'. This echos the string parameter (for example cd myProjectFolderName [Y, n]) on the commandline, after that it waits for your input. If you type Y or simply press enter, the script goes on (with executing this command). If you type "n", the script stops.

该函数call执行函数“go_on”。这会cd myProjectFolderName [Y, n]在命令行上回显字符串参数(例如),然后等待您的输入。如果您键入 Y 或简单地按 Enter,脚本将继续运行(执行此命令)。如果您键入“n”,脚本将停止。