【问题标题】:How to make bash scripts to automatically add and download files from github?如何制作 bash 脚本以自动从 github 添加和下载文件?
【发布时间】:2021-09-13 05:26:15
【问题描述】:

我在 linux 服务器上有一个克隆目录。已配置密钥访问。在服务器上进行一些处理后,在输出端,我得到了需要添加到 git 的 csv 文件。我还需要在服务器上接收 git 本身已更改的文件(这些文件可以是新添加的文件,也可以是代码中的一些更改)。添加过程每天都在发生,所以我想自动化它。在其中一个网站上,我找到了一个适合我的示例。向服务器添加和克隆更改的两个脚本。启动自动化通过 crontab 进行。

代码文件:

#!/bin/bash
# Go to the GIT category
cd '/home/user/www/'

# Submitting changes to the main branch
git checkout main
git add -A
git commit -m "update main"
git push

还有……

#!/bin/bash
# Go to the GIT category
cd '/home/user/www/'

# Loading data from the main branch
git checkout main
git pull

但我在输出中收到以下错误,可能是什么问题?

Cronlog:

/home/user/RFFI-V/Work/start_git_download.sh: line 3: cd: too many arguments
/home/user/RFFI-V/Work/start_git_push.sh: line 3: cd: too many arguments
fatal: not a git repository (or any parent up to mount point /)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
fatal: not a git repository (or any parent up to mount point /)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
fatal: not a git repository (or any parent up to mount point /)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
fatal: not a git repository (or any parent up to mount point /)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
fatal: not a git repository (or any parent up to mount point /)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
fatal: not a git repository (or any parent up to mount point /)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
fatal: not a git repository (or any parent up to mount point /)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).

【问题讨论】:

  • 如果你只是在目标机器上运行cd '/home/user/www/' 命令呢? (以root身份运行,crontab以root身份运行...)
  • 错误消息cd: too many arguments 似乎与问题中显示的脚本不匹配。您是否复制并粘贴了您实际使用的代码?对于调试,将第一行更改为 #!/bin/bash -x 并检查日志中的结果输出。
  • 是 git init 丢失还是你正在克隆这个目录?
  • 如图所示,我认为这些应该可以工作。我不得不说这很像 Windows 风格的换行符。你的脚本有可能是 CRLF 吗?如果是这样,bash 不兼容,转换为 Unix 风格的 LF。
  • 不要这样做。使用 git 存储非人为生成的内容确实不是一个好主意,无论它多么普遍。

标签: linux bash git


【解决方案1】:

手动运行每个命令,看看您是否收到任何问题。

如果您没有收到任何问题,那么 bash 脚本是否位于 crontab 的正确文件夹中,并且默认情况下该脚本将以 root 身份运行,如果您想以其他用户身份运行脚本,请提供设置脚本的示例语法在 crontab 中以不同用户身份运行如下所示,

1 2 3 4 5 USERNAME /path/to/script.sh

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-16
    • 2017-03-29
    • 1970-01-01
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多