.sh在windows在编辑可能会带来的问题

今天在实现一个 shell 自动提交的问题的时候,遇到一个有些坑的问题。

1 前提描述

  1. 我渐渐习惯使用 Win10 Linux Bash Shell,用的是 Ubuntu 18
  2. 通过 Win10 bash,建立文件,在win10中,通过 notepad++ 编辑文件
  3. 通过 notepa++ 修改 ubuntu 下的编程文件,除了偶然会修改掉文件的权限,没有大的问题

2 问题

问题出在我打算实现一个 自动发布和提交的 .sh 文件的时候出了问题

  1. 通过 git 管理文档每次提交推送都会执行的三个命令
1
2
3
git add .
git commit -m "XXXX"
git push

我寻思着,我这个hexo博客,没有每次都这样执行这三条,另外,hexo的每次编译,发布的命令也挺烦人

  1. 在 Ubuntu 下建好,gitpush.sh ,退回 win10中,在 notepad++ 添加以下内容
1
2
3
4
5
6
7
8
9
10
time=$(date "+%Y%m%d-%H%M%S")
if [ -n "$time" ]; then
git add -A
git commit -m"${time}"
git push
git status
echo "完成push"
else
echo "请添加注释再来一遍"
fi

这个是以时间戳,为内容提交和推送

在执行的时候遇到问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
XXX@XXX-x1:~/gitfile/blog$ sudo ./gitpush.sh
error: unknown option `all?'
usage: git add [<options>] [--] <pathspec>...

-n, --dry-run dry run
-v, --verbose be verbose

-i, --interactive interactive picking
-p, --patch select hunks interactively
-e, --edit edit current diff and apply
-f, --force allow adding otherwise ignored files
-u, --update update tracked files
--renormalize renormalize EOL of tracked files (implies -u)
-N, --intent-to-add record only the fact that the path will be added later
-A, --all add changes from all tracked and untracked files
--ignore-removal ignore paths removed in the working tree (same as --no-all)
--refresh don't add, only refresh the index
--ignore-errors just skip files which cannot be added because of errors
--ignore-missing check if - even missing - files are ignored in dry run
--chmod <(+/-)x> override the executable bit of the listed files

On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
modified: README.md
modified: themes/sky (modified content)

Untracked files:
gitpush.sh

no changes added to commit
' is not a git command. See 'git --help'.

The most similar command is
push
' is not a git command. See 'git --help'.

The most similar command is

遇到这个问题我一开始的方向是以为 shell 命令没有写对,找了好一通的各种写法尝试

最后的解决:回到 ubuntu 下用 vim 编写,保存就不会有这些问题

3 尾声

解决掉这个问题后其他的就很顺利,从体验上来说,有了 shell 确实让人更舒服了

4 两个sh的实现

  • gitpush.sh git 的shell 提交

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    time=$(date "+%Y%m%d-%H%M%S")
    if [ -n "$time" ]; then
    git add -A
    git commit -m"${time}"
    git push
    git status
    echo "完成push"
    else
    echo "请添加注释再来一遍"
    fi
  • hexo 的编译和 ftp 发布

    1
    2
    3
    sh gitpush.sh #先执行git提交
    hexo generate
    hexo deploy
  • 自此,在编辑完此文后,可以执行下面的命令来完成 备份和发布了

    1
    ./release.sh