【问题标题】:Linux Ubuntu: Script works in terminal, but not .shLinux Ubuntu:脚本在终端中工作,但不是 .sh
【发布时间】:2012-11-05 19:12:15
【问题描述】:

问题摘要:我的脚本在终端输入时可以正常工作,但是在终端中从 .sh 文件执行时却不能正常工作,这是为什么呢?

脚本:

echo World of Clucky - Frisnuk "\033]0;Frisnuk - World of Clucky\a"
#! /usr/bin/env bash
BINDIR="$(dirname "$(readlink -fn "$0")")"
cd "$BINDIR"
while true
do
source /home/clucky/MinecraftServers/Frisnuk/serverconfig/config.sh
#Start Server
    java -Xms2000M -Xmx2000M -jar $serverjar.jar nogui
    if [ "`date +%w%H`" = "001" ]
    then
#Delete map files for The End
    rm -R /Frisnuk_the_end
    echo "End has been successfully reloaded"
    echo "[`date +%D\ %T`] End Reloaded" >> /home/clucky/MinecraftServers/Frisnuk/EndRestart.txt
#Change Item of The Week
    weekofyear=`date +%y\-%U`
    s=$(<serverconfig/ItemofTheWeek/item$weekofyear.txt)
    set -- $s
    itemoftheweekid=$2
    itemoftheweekprice=$3
    xmlstarlet edit -L -u "/scs-shop/itemStack[@type='double']" -v $itemoftheweekid /plugins/ShowCaseStandalone/ffs-storage/ffss_cac8480951254352116d5255e795006252d404d8
    xmlstarlet edit -L -u "/scs-shop/price[@type='double']" -v $itemoftheweekprice /plugins/ShowCaseStandalone/ffs-storage/ffss_cac8480951254352116d5255e795006252d404d8
fi
echo "If you want to stop the restart and shut the server off instead, please press Ctrl+C at this time"
for i in 5 4 3 2 1
do
    echo "$i..."
    sleep 1
done
echo "Restarting Server"
clear
done

它没有工作和运行服务器,而是这样说:

World of Clucky - Frisnuk 
/home/clucky/MinecraftServers/Frisnuk/craftminecraft.sh: 7: /home/clucky/MinecraftServers/Frisnuk/craftminecraft.sh: source: not found

Error: Unable to access jarfile .jar
If you want to stop the restart and shut the server off instead, please press Ctrl+C at this time
5...
4...
3...
2...
1...

我马上要洗个澡,但我会在今晚晚些时候或明天早上回来。非常感谢您的帮助。

【问题讨论】:

  • 可以在终端中找到源,但在脚本中找不到。文件位置是正确的,因为我只是将此脚本复制并粘贴到终端窗口中即可。
  • 脚本没有在任何地方定义$serverjar 变量。也许你已经在你的环境中定义了它?
  • $serverjar 在它所采购的脚本中定义。

标签: linux bash shell ubuntu terminal


【解决方案1】:

您在 shebang 之前添加了 echo,因此您的脚本将由 dash 解释,而不是 bash

dash 不包括source,因为它不是标准的。

纠正你的shebang,它会成功的。


source 脚本的标准方法是使用 . 执行它。

你用. ./myScript.sh代替source ./myScript.sh。它们在bash 中是相同的。

【讨论】:

    猜你喜欢
    • 2022-01-01
    • 1970-01-01
    • 2018-02-03
    • 1970-01-01
    • 1970-01-01
    • 2014-06-27
    • 2022-01-27
    • 2013-10-05
    • 2017-07-26
    相关资源
    最近更新 更多