AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 665854
Accepted
ReynierPM
ReynierPM
Asked: 2015-02-08 12:32:16 +0800 CST2015-02-08 12:32:16 +0800 CST 2015-02-08 12:32:16 +0800 CST

在 bash 中执行脚本期间命令失败

  • 772

我正在编写一个用于自动化一些常见任务的 bash 脚本,但我遇到了一些问题,我需要一些帮助。这是我正在谈论的脚本:

#!/usr/bin/env bash

PS3='Please enter your choice: '
options=("Prepare environment" "Create new group" "Add users to group" "Change directory ownership" "Change directory permissions" "Quit")
select opt in "${options[@]}"
do
    case $opt in
        "Prepare environment")
            read -e -p "Enter the work directory: " -i "/var/www/html/" directory
            cd directory

            echo "Updating BOWER..."
            npm -g update bower

            echo "Updating COMPOSER..."
            composer self-update

            read -e -p "Enter the environment: " -i "prod" environment

            if [environment eq prod]
            then
                git fetch --all
                git reset --hard origin/master
                git pull

                composer install --no-dev --optimize-autoloader
                php app/console cache:clear --env=prod --no-debug
            else
                read -e -p "Enter the environment type: " -i "local" envtype

                if [envtype eq local]
                then
                    composer update
                    php app/console cache:clear --env=dev
                    php app/console cache:warmup
                else 
                    git fetch --all
                    git reset --hard origin/master
                    git pull

                    composer update
                    php app/console cache:clear --env=dev
                    php app/console cache:warmup
                fi  
            fi          

            echo "Cleaning the house and updating libraries..." 
            bower cache clean --allow-root
            bower prune --allow-root
            bower install --allow-root
            bower update --allow-root
        ;;
        "Create new group")
            read -e -p "Enter the group name: " -i "www-pub" groupname
            groupadd groupname
            echo "You have added a new group: " groupname
            ;;
        "Add users to group")
            read -e -p "Enter the group name: " -i "www-pub" groupname
            loop=true          # "true" is a command

            while true; do
                read -p "enter username: " username
                [[ -z $username ]] && break

                usermod -a -G groupname username # add user to group
                echo "You have added: " username " to group: " groupname
            done        
            ;;
        "Change directory ownership")
            read -e -p "Enter the group name: " -i "www-pub" "Enter the directory: " -i "/var/www/html" groupname directory
            chown -R root:groupname directory
            echo "You have changed the ownership for: " directory " to root:" groupname
            ;;
        "Change directory permissions")
            read -e -p "Enter the directory: " -i "/var/www/html" "Enter the directtory permissions (type -d) : " -i "2775" "Enter the directtory files (type -f) : " -i "0664"  directory folder files
            echo "Setting permissions " folder " to " directory
            find directory -type d -print0 | xargs -0 chmod permissions 
            echo "Setting permissions " files " to " directory " files"
            find directory -type f -print0 | xargs -0 chmod files 
            ;;
        "Quit")
            break
            ;;
        *) echo invalid option;;
    esac
done

每当我运行脚本时,我都会得到以下输出:

root@test-webserver:/var/www/sis-php-source# /home/script.sh
1) Prepare environment           4) Change directory ownership
2) Create new group              5) Change directory permissions
3) Add users to group            6) Quit
Please enter your choice: 1
Enter the work directory: /var/www/sis-php-source/
/home/script.sh: line 10: cd: directory: No such file or directory
Updating BOWER...
Updating COMPOSER...
You are already using composer version 07c644ac229a21df80180598d8bb9aaba232eecb.
Enter the environment: prod
/home/script.sh: line 20: [environment: command not found
Enter the environment type: local

为什么cd命令失败?如果脚本是输入变量,为什么environment脚本会尝试environment作为命令执行?任何人都可以给我一些帮助吗?

linux
  • 1 1 个回答
  • 543 Views

1 个回答

  • Voted
  1. Best Answer
    glenn jackman
    2015-02-08T14:32:54+08:002015-02-08T14:32:54+08:00

    在 bash 中,您可以使用$directory. 手册中的所有细节。

    除非您明确知道何时取消引号,否则您应该始终引用"$variable"扩展。

    另外,[...]不是语法,[实际上是一个命令(命令的别名test)。命令必须用空格与其参数分开。

    if [ "$environment" = prod ]
    

    看:

    • https://www.gnu.org/software/bash/manual/bashref.html#Conditional-Constructs
    • https://www.gnu.org/software/bash/manual/bashref.html#Bash-Conditional-Expressions
    • 2

相关问题

  • Linux 主机到主机迁移

  • 如何在 Linux 机器上找到有关硬件的详细信息?

  • 如何在 Linux 下监控每个进程的网络 I/O 使用情况?

  • 在 RHEL4 上修改 CUPS 中的现有打印机设置

  • 为本地网络中的名称解析添加自定义 dns 条目

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve