首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >命令在Bash脚本中找不到错误

命令在Bash脚本中找不到错误
EN

Ask Ubuntu用户
提问于 2019-03-18 03:17:03
回答 1查看 5.8K关注 0票数 2

因此,我编写了一个bashscript.sh文件,以检查在克隆项目后目录project1_repo是否为空。

我已经编写了四个不同的函数来验证它,但是一直都有command not found错误。如果有syntax error,我已经检查了好几次,但都是徒劳的。有人能帮帮我吗?谢谢。

编辑:以前由于一个错误,project1_install_dir被称为colsim1_install_dir,但是编辑的版本是正确的。

代码语言:javascript
复制
#!/bin/bash

#path to install project1
function project1_install_dir() {
   while true; 
 do
  read -p "Enter FULL folder path where you want to install project1:" fullpath
  echo "you have enterd $fullpath. Please press 'y' to confirm and 'n' to enter again"
  read -p "Continue? (Y/N): " confirm 
  if [[ $confirm =~ ^([yY][eE][sS]|[yY])$ ]]; then
    break
  else
    continue         
  fi
done
 }

#clone project1
function clone_project1_repo() {
  git clone example git .     
    }

# four functions to Check whether cloning is successful
# function 1
function success_of_cloning_of_project1_repo3() {
  if find $fullpath/project1/project1_repo -mindepth 1 | read; then
     echo "dir not empty"
  else
     echo "dir empty"
  fi
}

# function 2
function success_of_cloning_of_project_repo2() {
DIR="$fullpath/project1/project1_repo"
if [ -n "$(ls -A $DIR)" ]; then
  echo "Take action $DIR is not Empty"
else
  echo "$DIR is Empty"
fi
}

# function 3    
function success_of_cloning_of_project_repo1() {
if [ -d $fullpath/project1/project1_repo ]; then
      [ -n "$(ls -A $fullpath/project1/project1_repo)" ] && echo "Not Empty" || echo "Empty"
else
   :
fi
    }

# function 4
function success_of_cloning_of_project_repo() {
while true;
 do
  if [ -n "$(ls -A $fullpath/project1/project1_repo)" ]; then
    echo "cloning of project1_repo is successful"               
    break
  else
    echo "cloning of project1_repo is NOT successful."
    continue      
  fi                
 done
}

#calling the functions
    function main() {
       project1_install_dir
       success_of_cloning_of_project1_repo3 
       success_of_cloning_of_project1_repo2  
       success_of_cloning_of_project1_repo1
       success_of_cloning_of_project1_repo
    }

    main

终端输出:

代码语言:javascript
复制
jen@ex343:tdk/jen$ source bash_file_test.sh 
Enter FULL folder path where you want to install project1:/tdk/jen

you have enterd /tdk/jen. Please press 'y' to confirm and 'n' to enter again
 Continue? (Y/N): y
 You have chosen yes
-bash: success_of_cloning_of_project1_repo3: command not found
-bash: success_of_cloning_of_project1_repo2: command not found
-bash: success_of_cloning_of_project1_repo1: command not found
-bash: success_of_cloning_of_project1_repo: command not found
EN

回答 1

Ask Ubuntu用户

发布于 2019-03-18 10:52:18

将代码粘贴到https://www.shellcheck.net/报表中:

代码语言:javascript
复制
$ shellcheck myscript

Line 7:
  read -p "Enter FULL folder path where you want to install project1:" fullpath
  ^-- SC2162: read without -r will mangle backslashes.

Line 9:
  read -p "Continue? (Y/N): " confirm
  ^-- SC2162: read without -r will mangle backslashes.

Line 26:
  if find $fullpath/project1/project1_repo -mindepth 1 | read; then
          ^-- SC2086: Double quote to prevent globbing and word splitting.
                                                         ^-- SC2162: read without -r will mangle backslashes.

Did you mean: (apply this, apply all SC2086)
  if find "$fullpath"/project1/project1_repo -mindepth 1 | read; then

Line 36:
if [ -n "$(ls -A $DIR)" ]; then
                 ^-- SC2086: Double quote to prevent globbing and word splitting.

Did you mean: (apply this, apply all SC2086)
if [ -n "$(ls -A "$DIR")" ]; then

Line 45:
if [ -d $fullpath/project1/project1_repo ]; then
        ^-- SC2086: Double quote to prevent globbing and word splitting.

Did you mean: (apply this, apply all SC2086)
if [ -d "$fullpath"/project1/project1_repo ]; then

Line 46:
      [ -n "$(ls -A $fullpath/project1/project1_repo)" ] && echo "Not Empty" || echo "Empty"
                    ^-- SC2086: Double quote to prevent globbing and word splitting.

Did you mean: (apply this, apply all SC2086)
      [ -n "$(ls -A "$fullpath"/project1/project1_repo)" ] && echo "Not Empty" || echo "Empty"

Line 56:
  if [ -n "$(ls -A $fullpath/project1/project1_repo)" ]; then
                   ^-- SC2086: Double quote to prevent globbing and word splitting.

Did you mean: (apply this, apply all SC2086)
  if [ -n "$(ls -A "$fullpath"/project1/project1_repo)" ]; then

您可以按照建议使用"$fullpath"和上面注释中的任何其他建议。在修复当前错误ShellCheck报告之后,当您再次运行它时,它可能会报告其他错误。

票数 4
EN
页面原文内容由Ask Ubuntu提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://askubuntu.com/questions/1126511

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档