git bash创建脚本及其他

本文介绍如何在GitBash环境下创建并运行shell脚本,包括使用touch命令创建脚本文件、通过修改PATH环境变量使得脚本可以在任意目录下被调用的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

git bash创建脚本

  • 创建脚本

    touch xxx.sh
  • 之后用 xxx.sh 脚本的绝对路径来访问

    sh /x/x/xxx.sh
  • 或者,将脚本绝对路径添加进PATH里面(这部分命令区分大小写)

    1. PATH就是 git bash 用来搜索指令的路径集
    2. 思路就是把 xxx.sh 文件所在文件夹加到路径集里,使输入 xxx.sh 可以搜索这个路径,找到脚本
    3. 查看当前路径集

      echo $PATH
    4. 在脚本绝对路径下创建.bashrc的文件

      cd /x/x/
      touch .bashrc
    5. 编辑.bahsrc文件内容如下(区分大小写):

      vi .bashrc 或者 代码编辑器里编写 .bashrc 文件
      export PATH="脚本绝对路径:$PATH"
    6. 进入git bash,生效.bashrc文件

      source ./.bashrc
    7. 之后可以使用脚本文件名 xxx.sh 来执行脚本

git bash编辑器vim的一些点

  • [ -d 目录名 ] 检验目录是否存在
  • 条件语句

    if [ ]; then
    elif [ ]; then
    elif [ ];then
    fi

git bash的一些点

  • 进入自带的vim编辑器

    vi xxx
  • type
  • which
  • echo 的用法

    • 例子:

      echo -e "abc\ndef"
      output:
          abc
          def
      echo -n "abc"
      -n do not output the trailing newline
      echo本身默认最后会输出一个换行,使用 -n 来禁用最后的换行
    • echo 具体语法如下:

      • Linux echo命令不能显示文件中的内容。
      • 功能说明:显示文字。
      • 语法:echo [-ne] [字符串] 或 echo [--help] [--version]
      • 参数:

        -n 不要在最后自动换行
        -E 不启用转义,默认项
        -e 启用转义,若字符串中出现以下字符,则特别加以处理,而不会将它当成一般文字输出: 
            \a 发出警告声;
            \b 删除前一个字符;
            \c 最后不加上换行符号;
            \f 换行但光标仍旧停留在原来的位置;
            \n 换行且光标移至行首;
            \r 光标移至行首,但不换行;
            \t 插入tab;
            \v 与\f相同
Git指令的Shell脚本,能够快速便捷地管理Git库,包括添加修改、提交修改、显示库状态、推送到远程库、从远程库更新到本地、版本恢复等操作。 使用方法: 1. 在Linux系统中,将本文件放在Git库目录下,利用Shell运行本文件; 2.在windows系统中,需下载安装与操作系统相对应的Git软件,并将本文件放在Git库目录下,双击即可运行。 运行示例: Please choose the first letter of options. [Add|Commit|Diff|Fetch|Exit|Help|Log|Push|User|Reset|Status]? h A: Add all changes to repository. C: Commit all changes to repository. D: Show differences between current version with HEAD->. E: Exit shell script. F: Fetch origin/master and merge. L: Show latest two-weeks logs of repository. P: Push commissions to origin/master. U: User command mode(Press ‘Enter’ to exit). R: Reset current version according version_id. S: Show status of repository. Please choose the first letter of options. [Add|Commit|Diff|Fetch|Exit|Help|Log|Push|User|Reset|Status]? s On branch master Your branch is up-to-date with 'origin/master'. Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: Git.sh modified: PyNote/PyNote_2.md no changes added to commit (use "git add" and/or "git commit -a") Please choose the first letter of options. [Add|Commit|Diff|Fetch|Exit|Help|Log|Push|User|Reset|Status]? a On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD ..." to unstage) modified: Git.sh modified: PyNote/PyNote_2.md
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值