简体   繁体   English

Linux Ubuntu:脚本在终端中工作,但不在.sh中工作

[英]Linux Ubuntu: Script works in terminal, but not .sh

Issue Summary: My script works as it should when typed into the terminal, however, it does not work correctly when executed in terminal from a .sh file, why is this? 问题摘要:我的脚本在输入到终端时应该正常工作,但是,当从终端从.sh文件执行时,它无法正常工作,为什么会这样?

Script: 脚本:

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

Instead of working and running the server, it just says this: 它没有工作和运行服务器,而是说:

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...

I am going to take a shower shortly, but I will be returning either later tonight, or tomorrow morning. 我很快就会洗澡,但是我会在今晚或明天早上回来。 Thank you in advanced for your assistance. 谢谢你的帮助。

You put an echo before the shebang, so your script is being interpreted by dash , not bash . 你在shebang之前放了一个echo ,所以你的脚本是由dash解释的,而不是bash

dash doesn't include source , because it's not standard. dash不包括source ,因为它不是标准的。

Correct your shebang and it'll do the trick. 纠正你的shebang,它会做的伎俩。


The standard way to source a script is executing it with . source脚本的标准方法是使用它执行它. .

Instead of source ./myScript.sh , you do . ./myScript.sh 而不是source ./myScript.sh ,你做. ./myScript.sh . ./myScript.sh . . ./myScript.sh They're the same in bash . 他们在bash是一样的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM