自编Shell脚本实现Typora+Gitee图床配置

一、搭建Gitee图床

新建Gitee仓库

安装Git

  • git官网上下载相应版本的git

  • 安装时一般一直点Next就好

  • 安装完成后,注意配置环境变量:在这里插入图片描述

  • 打开cmd,输入git --version,若出现如下提示则表明git安装成功:在这里插入图片描述

  • 输入bashsh,若呈现如下样式则表明环境变量配置成功:在这里插入图片描述

生成/添加SSH公钥

  • 为了免密使用码云上传和下载代码,需要配置git ssh key。输入:

    ssh-keygen -t rsa -C "你的邮箱"
    
  • 一路回车,使用默认值即可,不用设置密码

  • Windows下公钥文件的路径是C:\Users\10147\.ssh在这里插入图片描述

  • 用记事本打开该文件,复制里面的内容

  • 回到Gitee中,在【个人设置】——【安全设置】——【SSH公钥】中,添加公钥,将复制的内容粘贴到公钥中。

  • 添加后,在终端中输入ssh -T [email protected],出现如下提示表明公钥添加成功:

新建本地仓库

  • 选择合适的路径,作为Gitee图床的本地仓库

  • 【右键】——【Git Bash Here】

  • 输入:

    git init
    git remote add origin https://gitee.com/你的Gitee图床仓库地址
    git pull orgin master
    

测试图片推送

  • 你可以在本地仓库中随意添加一张图片,然后输入:

    git add .
    git commit -m "第一次上传"
    git push origin master
    
  • 进入Gitee图床中,看看是否有你刚才添加的图片,如果有,则说明之前的步骤都操作正确。

二、编写图片上传Shell脚本

  • 在你的本地仓库路径下新建upload.sh文件,内容如下:

    if [[ $# -eq 0 ]];then
            echo "No Image Input"
            exit 2
    fi
    
    # 本地仓库地址
    local_repo_path="你的本地仓库地址"
    # 远程Git仓库链接
    origin_repo_path="https://gitee.com/你的Gitee图床仓库地址"
    
    cd $local_repo_path
    
    # 拷贝图片到本地git仓库并打印链接
    img_path=""
    while [[ $# -gt 0 ]]
    do
            path_raw=$1
            img_path=$path_raw
            shift
            
            # 判断是否是完整路径(可能含有空格)
            img_type=${
          
          path_raw##*.}
            while [[ $# -gt 0 && "${img_type}" != "jpg" && "${img_type}" != "jpeg" && "${img_type}" != "png" && "${img_type}" != "bmp" && "${img_type}" != "gif" && "${img_type}" != "tiff" && "${img_type}" != "svg" ]]
            do
                    img_path="$img_path $1"
                    img_type=${
          
          img_path##*.}
                    shift
            done
            
            # 拷贝
            cp "$img_path" ./img/
    
            # 打印图片链接
            echo ${origin_repo_path}${
          
          img_path##*[\\\/]}
            img_path=""
    done
    
    # 上传到远程仓库
    git add img/
    git commit -m "upload image"
    git push
    
  • 注意在脚本中设置自己的本地和远程仓库地址!

  • 脚本中仅支持jpg、jpeg、png、bmp、gif、tiff、svg这几种格式的图片,如有需要可以自己添加。

三、Typora设置图片上传

  • 在Typora中,【文件】——【偏好设置】——【图像】,插入图像时可以设为上传图像,也可以设为无特殊操作,这样上传图像需要手动操作。上传服务设为Custom Command,命令设为sh C:\\Users\\10147\\Pictures\\image\\upload.sh(你的脚本路径)在这里插入图片描述

  • 点击验证图片上传选项,若出现如下提示则说明Typora获取到了图像链接,但不一定代表图片真正上传到了Gitee仓库中:在这里插入图片描述

  • 点击图片的URL,若能正确显示出图像,则说明正确上传到了Gitee图床中。也可以进入你的Gitee图床中看是否存在这两张图片。

猜你喜欢

转载自blog.csdn.net/qq_34731182/article/details/113336224