无法在 Bash 中从用户输入读取变量

无法在 Bash 中从用户输入读取变量

我尝试运行我朋友的脚本,但尝试输入一些变量时出现此错误:

: bad variable nameead: reload
: not foundsh: 10: bin/reload.sh: 
bin/reload.sh: 11: bin/reload.sh: Syntax error: word unexpected (expecting "in")

问题出在哪里?下面是sh脚本的代码:

#!/bin/bash

echo ""
echo "Выберите необходимое действие:"
echo "    1 - reload."
echo "    2 - db."
echo "    3 - tests."
echo "    0 - exit."
read reload

case $reload in
1)
echo "reload."
php composer.phar update
php vendor/sensio/distribution-bundle/Sensio/Bundle/DistributionBundle/Resources/bin/build_bootstrap.php

rm -rf app/cache/*
rm -rf app/logs/*
touch app/logs/error.log

APACHEUSER=`ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data' | grep -v root | head -1 | cut -d\  -f1`

mkdir -p web/upload/media
rm -rf web/upload/media/*
rm -rf web/upload/dream/*
rm -rf web/upload/tmp/*

setfacl -R -m u:"$APACHEUSER":rwX -m u:`whoami`:rwX app/cache app/logs web/upload
setfacl -dR -m u:"$APACHEUSER":rwX -m u:`whoami`:rwX app/cache app/logs web/upload

php app/console doctrine:database:drop --force
php app/console doctrine:database:create
php app/console doctrine:schema:update --force
php app/console cache:clear
php app/console doctrine:fixtures:load --no-interaction
php app/console assets:install --symlink
php app/console assetic:dump
php app/console cache:clear
;;
2)
echo "reload db."
php app/console doctrine:database:drop --force
php app/console doctrine:database:create
php app/console doctrine:schema:update --force
php app/console doctrine:fixtures:load --no-interaction
php app/console assets:install --symlink
php app/console assetic:dump
php app/console cache:clear
;;
3)
echo ""
echo "tests:"
echo "    1 - Запуск всех тестов."
echo "    2 - Запуск юнит тестов."
echo "    0 - Выход."
read testing

case $testing in
1)
    echo "Запуск всех тестов."
    php app/console doctrine:database:drop --force
    php app/console doctrine:database:create
    php app/console doctrine:schema:update --force
    php app/console cache:clear
    php app/console doctrine:fixtures:load --no-interaction
    php app/console cache:clear

    sh bin/tests.sh
;;
2)
    echo "Запуск юнит тестов."
    php app/console doctrine:database:drop --force
    php app/console doctrine:database:create
    php app/console doctrine:schema:update --force
    php app/console cache:clear
    php app/console doctrine:fixtures:load --no-interaction
    php app/console cache:clear

    bin/phpunit -c app
;;
0)
    exit 0
;;
*)
    echo "Введите правильное действие!"
    sh bin/reload.sh

esac
;;
0)
exit 0
;;
*)
echo "Введите правильное действие!"
sh bin/reload.sh

esac

答案1

我解决了这个问题。这很简单。我需要将 .sh 文件中的字符串结尾更改为 Unix/Linux 格式,而不是 Windows 结尾。

相关内容