echo >> Gives an Error Saying the File Doesn't Exist - bash

According to research, echo >> should create the file if it doesn't exist. (EDIT!! echoing to a file in the base terminal works. For some reason, it doesn't work in the .sh.) It works on the integrated "git bash" terminal, but not in MSYS2 MINGW. And one of my friends said that it gave an error in Fedora as well. My code is trying to detect if the cloned repo is the right version, and it will git pull origin main if it is outdated. Here is the code.
#!/bin/bash
NC='\033[0m' #Reset text color.
P='\033[0;35m' #Purple
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')") #get the base directory of start.sh
cd $basedir
ver=$(sed '1!d' $(dirname $(pwd))/.version) #get local version
if ! [ -f .var/asked ]; then #this is in place so it only asks the below question once, ever.
if ! [ -f .var/vc ]; then #I don't know why this is here anymore
echo "Would you like ScratchLang to check its version every time you start it? [Y/N]"
read -sn 1 ff
if [ h$ff == hy ] || [ h$ff == hY ]; then
echo >>.var/vc "Don't remove this file please." #when start.sh detects this file, it will check for a new version.
fi
echo >>.var/asked "Don't remove."
fi
fi
if [ -f .var/vc ]; then
if ! [ h$1 == hnope ]; then
echo "Checking version..."
if [ -f .version ]; then
rm .version
fi
wget -q https://raw.githubusercontent.com/0K9090/ScratchLang/main/.version #get the .version file from github
utd=1
if ! [ "$ver" == "$(sed '1!d' .version)" ]; then #if local version doesn't match .version from github, then set utd to 0
utd=0
fi
if [ $utd == 0 ]; then #if utd = 0 then update
echo "Your version of ScratchLang ($ver) is outdated. The current version is $(sed '1!d' .version). Would you like to update? [Y/N]"
read -sn 1 hh
if [ h$hh == hy ] || [ h$hh == hY ]; then
git pull origin main
fi
exit
fi
rm .version
fi
fi

Related

why am i only getting the return of the help_syntax function when ran even with my args $1 and $2 completed?

My code will only provide two options. one) the create_file function or two) then help_syntax function. I cannot figure out why. I am very new to Bash scripting, in fact Kali Linux alone and I'm trying to make a code to append new aliases to the ~/.zsh_aliases file I've created. Please run this code and provide pointers for me in the right direction. Please fully explain you answer so I can learn from my mistakes as well.
I've been through different variations of the code, originally it was just a bunch of if and elif statements but I ran into an issue when i didn't know how to test the code for if this is that AND this is that_2 then execute so I changed to functions where I could separate into sections and execute accordingly.
#! /bin/bash
# Functions
function help_syntax () {
echo "Syntax: ./allyAppend.sh {alias_name} \"{command}\""
echo "Example: ./allyAppend.sh upug \"sudo apt-get update -y && sudo apt-get upgrade -y && sudo apt autoremove -y\""
}
function append () {
if [ -z $1 ];
then
help_syntax
elif [ ! -z $1 ] && [ ! -z $2 ];
then
echo "alias $1=\"$2\"" >> ./test
#source ~/.zshrc
fi
}
function create_file () {
if [ -z $1 ];
then
help_syntax
elif [ ! -z $1 ] && [ ! -z $2 ];
then
read -p "[X] Error:: ~/.zsh_aliases doesnt exsist. Create the file and add your alias [y/N]?: " add
if [ $add == "y" ]; then
touch ./test
echo "alias $1=\"$2\"" >> ./test
echo "[X] NOTICE: You must edit your ~/.zshrc file to allow the aliases file" >> ./README
echo " enter:: if [ -f ~/.zsh_aliases ]; then
. ~/.zsh_aliases
fi
:: at line 254 in ~/.zshrc and dont forget to tab in" >> ./README
fi
else
echo "[:(] Good Bye! Thank You"
fi
}
# Tests if alias file exsists
if [ -f "./test" ];
then
append
else
create_file
fi

How to import a custom file from .bashrc]

Over time size of both .bash_functions and .bash_aliases increased and so I decided to break them up:
.bash_functions into:
.bash_functions
.bash_functions-media
.bash_aliases into:
.bash_aliases
.bash_aliases-py_links
I also modified beginning of .bashrc in following way:
if [ -f ~/.bashdata ]; then
echo -n "sourcing .bashdata"
source /home/paul/.bashdata && echo "...done" || echo "...FAIL"
fi
if [ -f ~/.bash_aliases ]; then
echo -n "sourcing .bash_aliases"
source /home/paul/.bash_aliases && echo "...done" || echo "...FAIL"
fi
if [ -f ~/.bash_aliases-py_links ]; then
echo -n "sourcing .bash_aliases-py_links"
source /home/paul/.bash_aliases-py_links && echo "...done" || echo "...FAIL"
fi
if [ -f ~/.bash_functions ]; then
echo -n "sourcing .bash_functions"
source /home/paul/.bash_functions && echo "...done" || echo "...FAIL"
fi
if [ -f ~/.bash_functions-media ]; then
echo -n "sourcing .bash_functions-media"
source /home/paul/.bash_functions-media && echo "...done" || echo "...FAIL"
fi
The problem is .bash_functions-media and .bash_aliases-py_links are not sourcing from .bashrc. I can source them fine once terminal is up.
It's even more mind boggling, because .bashdata is custom file too and it sources fine.
I struggled with this for a while, and after removing hyphen from both filenames...
.bash_aliases-py_links -> .bash_aliases_py_links
.bash_functions-media -> .bash_functions_media
...both files sourced fine. Perhaps it will help someone with similar issue.
I found this solution totally by accident and was surprised when it worked, because hyphen is allowed in filenames.

Bash - Text stored in variable is not recognized when put it as argument

I am trying to make a script that automates download of SpigotMC BuildTools, detect input version ( sh build.sh <version> ) , and set a variable which is used if the version is greater or equal with 1.14 (since this release, SpigotMC discontinued the automation of auto-compilation of craftbukkit by default. Now it compiles only the spigot jar and I need both of them compiled for my personal purposes.)
Here is what I have tried (I'm a Linux newbie, so this might be messed up):
#!/bin/bash
# Sources:
# Append variables in bash : https://www.cyberciti.biz/faq/howto-linux-unix-bash-append-textto-variables/
# Check MANIFEST.MF content from jar file : https://www.manongdao.com/q-106728.html
# Check for specified property json file : https://stackoverflow.com/questions/34543829/jq-cannot-index-array-with-string
# https://www.ultralinux.org/post/json-bash/
# Bash Array : https://unix.stackexchange.com/questions/253892/syntax-error-unexpected-when-creating-an-array
# #######################################################################################################################################
#
# #######################################################################################################################################
# BuildTools Jar Info Variables
# -----------------------------
# BuilsTools working directory
buildtools_root="$(pwd)" # We use $(command) to store "command" output in a variable. We use ${command} if we have to stick other strings near it without whitespace.
# BuildTools jar location
buildtools_jar="$buildtools_root/BuildTools.jar"
# Current BuildTools jar version
buildtools_ver="$(unzip -p "$buildtools_jar" META-INF/MANIFEST.MF | grep 'Implementation-Version:' | cut -d '-' -f 5)"
# Jenkins Api-related variables.
# A. Retrieve BuildTools lastSuccessfulBuild link
targetJar="$(curl -s 'https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/api/json' | jq -r '.artifacts[].relativePath')"
lastSuccessfulBuild="$(curl -s 'https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/api/json' | jq -r '.url')"
# B. Make the download link
latestBuildToolsUrl="${lastSuccessfulBuild}artifacts/$targetJar"
# Latest BuildTools Build Version
latestBuildToolsVersion="$(curl -s 'https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/api/json' | jq -r '.number')"
displayFullName="$(curl -s 'https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/api/json' | jq -r '.fullDisplayName')"
# ---------------------------------------------------------------------------------------------------------------------------------------
# BuildTools Jar Run Variables
# -----------------------------
#
# #######################################################################################################################################
# This is a function to set java home to current java version in use.
javahome_set () {
JAVA_HOME=$(dirname "$(dirname "$( readlink -f /etc/alternatives/java )")")
OIFS=$IFS
IFS=':';
for i in $VAR;
do
JAVA1=$i/bin/java
JAVA2=$i/java
if [ -d "$i" ];
then
if [ ! -L "$JAVA1" ] && [ -x "$JAVA1" ] || [ ! -L "$JAVA2" ] && [ -x "$JAVA2" ]; then
echo "dropping path: $i";
else
NEW=$NEW:$i
fi
fi
done
IFS=$OIFS
JAVA_HOME=$NEW:$JAVA_HOME/bin
JAVA_HOME=${JAVA_HOME#:*}
}
javahome_set
# This function requires arguments. Checks if $1 >= $2
vercomp () {
if [[ $1 == $2 ]]
then
return 0
fi
local IFS=.
local i ver1=($1) ver2=($2)
# fill empty fields in ver1 with zeros
for ((i=${#ver1[#]}; i<${#ver2[#]}; i++))
do
ver1[i]=0
done
for ((i=0; i<${#ver1[#]}; i++))
do
if [[ -z ${ver2[i]} ]]
then
# fill empty fields in ver2 with zeros
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]}))
then
return 1
fi
if ((10#${ver1[i]} < 10#${ver2[i]}))
then
return 2
fi
done
return 0
}
# This is a function to download the latest BuildTools version and check if download is successful.
buildtools_download () {
curl --silent "$latestBuildToolsUrl" --output BuildTools.jar #Request download to BuildTools.
if [ "$?" -eq 0 ]; then #Check if file was downloaded. It returns error code non-zero if file was not properly downloaded. (in this language)
echo "$Green Successful downloaded BuildTools.$Color_Off"
else
echo "$Red Error while downloading BuildTools. $Color_Off"
fi
}
# This is a function to check for updates for BuildTools jar
buildtools_check () {
if [ ! -e "$buildtools_jar" ]; then #If BuildTools.jar does NOT exist in "BuildTools" folder
echo "$Yellow Downloading BuildTools..."
buildtools_download
exit
elif [ -e "$buildtools_jar" ]; then
if [ "$buildtools_ver" -lt "$latestBuildToolsVersion" ]; then
echo "$Blue Updating BuildTools... $Color_Off"
rm "$buildtools_jar"
buildtools_download
fi
else
echo "$Cyan BuildTools is up to date. $Color_Off"
fi
}
info_menu () {
echo "==============================================[Local-Info]=============================================="
echo "BuildTools Root : $buildtools_root"
echo "Executable Path : $buildtools_jar"
echo "Installed Version : $buildtools_ver"
echo "JAVA_HOME Directory : $JAVA_HOME"
echo "=============================================[JsonAPI-Info]============================================="
echo "Latest Download Url: $latestBuildToolsUrl"
echo "========================================================================================================"
echo
if [ "$buildtools_ver" -eq "$latestBuildToolsVersion" ]; then
echo "$Green You have the latest SpigotMC BuildTools version. $Color_Off"
else
echo "$Yellow A new build is available : $displayFullName"
fi
echo
}
if [ -z "$1" ]; then
echo "$BCyan Usage: $0 $BBlue<version> $Color_Off" & exit #By default, $0 is the name of this file
else
if [ -d "$1" ]; then #If argument is defined
if [ "$1" = "latest" ]; then
buildtools_check
else
vercomp "$1" "1.14"
if [ $? -eq 0 -o $? -eq 1 ]; then
BothJars=(--compile craftbukkit,spigot) # means $1 >= 1.14 ; sets the $BothJars variable
buildtools_check
fi
if [ "$1" -lt "1.14" ]; then
echo "$Yellow You want to build an older server version. Good choice btw."
buildtools_check
fi
fi
fi
rm -rf "$1" && mkdir "$1"
cd "$1" || exit
java -jar ./BuildTools.jar --rev "$1" "${BothJars[#]}" --generate-source --generate-docs #../filename is for executing it from one dir far.
I have put comments for you to understand what's going on.
Can someone help me with the problem?
EDIT : If you ever played Minecraft, or if you saw different versions of Minecraft, you can encounter the following version formats:
1.7.10 ; 1.8 ; 1.13.2 ; 1.14 ; 1.15.3 (and no, there is not a '1.8.0' version. Just '1.8')
Here are examples of comparisons between versions which are mathematical false after tr command :
1.8 > 1.7.10 - 18 > 1710
1.14 > 1.13.2 - 114 > 1132
1.15.2 < 1.16 - 1152 < 116
So... There may be problems that could affect the comparison of those numbers, since they are versions, not fractions or integers.
EDIT 2 : I will not make more than 3 edits / post. Thank you Richard K. for helping me with version checking function! I have rewritten my script to make it more complete. I store important parts in functions to save space and to be more visible.
Summary : I don't understand what have I done wrong here. U_U
EDIT 3 : As of Rachid K's answer, I got this part to test:
#!/bin/bash
vercomp () {
if [[ $1 == $2 ]]
then
return 0
fi
local IFS=.
local i ver1=($1) ver2=($2)
# fill empty fields in ver1 with zeros
for ((i=${#ver1[#]}; i<${#ver2[#]}; i++))
do
ver1[i]=0
done
for ((i=0; i<${#ver1[#]}; i++))
do
if [[ -z ${ver2[i]} ]]
then
# fill empty fields in ver2 with zeros
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]}))
then
return 1
fi
if ((10#${ver1[i]} < 10#${ver2[i]}))
then
return 2
fi
done
return 0
}
if [ -z "$1" ]; then
echo "$BCyan Usage: $0 $BBlue<version> $Color_Off" & exit #By default, $0 is the name of this file
else
if [ -d "$1" ]; then #If argument is defined
if [ "$1" = "latest" ]; then
echo "Building latest version"
else
vercomp "$1" "1.14"
rc=$?
case $rc in
0)
echo " You want to build version 1.14"
;;
1)
echo " You want to build version above 1.14"
;;
2)
echo " You want to build version below 1.14"
;;
latest)
echo " You want to build latest version."
;;
esac
fi
fi
fi
So the vercomp() should see if : $1 is equal with string latest ; $1 is greater or equal with 1.14 ; $1 is lower than 1.14.
The function looks good, but the echo command does not display. I noticed that in some cases the echo command doesn't display due to missing double quotes. So... putting that at it. But it doesn't work. Why?
Apparently the version you get is either a number like 1.14 or the "latest" string. You need to split you check in two tests. First you check the "latest" value :
if [ "$1" = "latest" ]
and if it is false, check the versions by using the solution proposed in this post
vercomp () {
if [[ $1 == $2 ]]
then
return 0
fi
local IFS=.
local i ver1=($1) ver2=($2)
# fill empty fields in ver1 with zeros
for ((i=${#ver1[#]}; i<${#ver2[#]}; i++))
do
ver1[i]=0
done
for ((i=0; i<${#ver1[#]}; i++))
do
if [[ -z ${ver2[i]} ]]
then
# fill empty fields in ver2 with zeros
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]}))
then
return 1
fi
if ((10#${ver1[i]} < 10#${ver2[i]}))
then
return 2
fi
done
return 0
}
So, to compare the version just do:
vercomp "$1" "1.14"
rc=$?
case $rc in
0) echo '=';;
1) echo '>';;
2) echo '<';;
esac
I don't see $BCyan and $BMagenta variables defined anywhere so there will just be blank text instead of them.
Problem with condition on line 7 is that you're comparing two string values but you in fact want to compare real numbers (or string "latest"). For real numbers it's best using bc utility. Here's the code equivalent to what you're trying to do (I think):
if [ `echo "$1>=1.14"|bc` -eq 1 ] || [ $1 == "latest" ]; then

shell script only runs as desired in certain directories

I am writing a bourne shell script that essentially has the same functionality as ls.
Here is my code.
#!/bin/sh
echo "\n"
if [ "$#" -eq 0 ]
then
SEARCH_DIR=`pwd`
fi
if [ "$#" -gt 0 ]
then
SEARCH_DIR=$1
if [ ! -d "$SEARCH_DIR" ]
then
echo "Directory Does Not Exist - - - Exiting"
echo "\n"
exit
fi
fi
DIR_CONTENT=`ls $SEARCH_DIR`
for file in $DIR_CONTENT
do
if [ -f "$file" ]
then
echo "f\c"
fi
if [ -d "$file" ]
then
echo "d\c"
fi
if [ ! -f "$file" ] && [ ! -d "$file" ]
then
echo "-\c"
fi
if [ -r "$file" ]
then
echo "r\c"
else
echo "-\c"
fi
if [ -w "$file" ]
then
echo "w\c"
else
echo "-\c"
fi
if [ -x "$file" ]
then
echo "x\c"
else
echo "-\c"
fi
echo ' \c'
echo "$file"
done
echo "\n"
When I execute the script, I get the desired output for that specific directory:
For example:
$ ./dirinfo
dirinfo version 0.1
drwx Desktop
frwx dirinfo
frw- #dirinfo#
frwx dirinfo~
frwx dirinfo2~
But if I try to pass an argument for a different directory the script doesn't seem to acknowledge my if statements.
For example:
$ ./dirinfo /bin
dirinfo version 0.1
---- bash
---- bunzip2
---- busybox
---- bzcat
---- bzcmp
But if I execute the script from the /bin directory I get the desired effect:
$ cd /bin
$ ~/dirinfo
dirinfo version 0.1
fr-x bash
fr-x bunzip2
fr-x busybox
fr-x bzcat
fr-x bzcmp
Could someone please attempt to point me in the right direction? Thanks!
Dont have a bash to test right now, but maybe $file doesnt have the full path so, evaluating -r or -w would not work. When you cd to destination directory, files are on ./.
Yes, as user430051 mentioned you are running it from a directory and listing files from another which will not work.
solution is prepend you search dir before filename like,
for file in $DIR_CONTENT
do
file="$SEARCH_DIR/$file"
if [ -f "$file" ]
then
echo "f\c"
fi
and it should work.
There can be many ways to solve your problem but the simplest solution would be to add one line which is missing (i.e cd "$SEARCH_DIR"), just add it in your script after DIR_CONTENT=ls $SEARCH_DIR and your script is good to go as per your expectation.
The main difference in this solution and above given by Nachiket is that in my solution file names in output will not have absolute path which I guess is your expectation.
DIR_CONTENT=`ls $SEARCH_DIR`
cd "$SEARCH_DIR"
for file in $DIR_CONTENT
do
if [ -f "$file" ]

Bash script expecting then, when else is needed

I am trying to run a shell script called graphhopper.sh in Ubuntu 12.04 which was given by a website. When I run it, terminal produces
: not found.sh: 2: graphhopper.sh:
graphhopper.sh: 39: graphhopper.sh: Syntax error: "else" unexpected (expecting "then")
The lines which start from 37 in the shell file are,
if [ ${OSM_FILE: -4} == ".pbf" ]; then
wget -O $OSM_FILE $LINK
else
# make sure aborting download does not result in loading corrupt osm file
TMP_OSM=temp.osm
wget -O - $LINK | bzip2 -d > $TMP_OSM
mv $TMP_OSM $OSM_FILE
fi
if [ ! -f "$OSM_FILE" ]; then
echo "ERROR couldn't download or extract OSM file $OSM_FILE ... exiting"
exit
fi
else
echo "## using existing osm file $OSM_FILE"
fi
This is the whole shell script.
#!/bin/bash
GH_HOME=$(dirname $0)
JAVA=$JAVA_HOME/bin/java
if [ "x$JAVA_HOME" = "x" ]; then
JAVA=java
fi
vers=`$JAVA -version 2>&1 | grep "java version" | awk '{print $3}' | tr -d \"`
bit64=`$JAVA -version 2>&1 | grep "64-Bit"`
if [ "x$bit64" != "x" ]; then
vers="$vers (64bit)"
fi
echo "## using java $vers from $JAVA_HOME"
CONFIG=config.properties
if [ ! -f "config.properties" ]; then
cp config-example.properties $CONFIG
fi
ACTION=$1
FILE=$2
USAGE="./graphhopper.sh import|ui|test <your-osm-file>"
if [ "x$ACTION" = "x" ]; then
echo -e "## action $ACTION not found. try \n$USAGE"
fi
function ensureOsmXml {
if [ ! -s "$OSM_FILE" ]; then
echo "File not found '$OSM_FILE'. Press ENTER to get it from: $LINK"
echo "Press CTRL+C if you do not have enough disc space or you don't want to download several MB."
read -e
echo "## now downloading OSM file from $LINK and extracting to $OSM_FILE"
if [ ${OSM_FILE: -4} == ".pbf" ]; then
wget -O $OSM_FILE $LINK
else
# make sure aborting download does not result in loading corrupt osm file
TMP_OSM=temp.osm
wget -O - $LINK | bzip2 -d > $TMP_OSM
mv $TMP_OSM $OSM_FILE
fi
if [ ! -f "$OSM_FILE" ]; then
echo "ERROR couldn't download or extract OSM file $OSM_FILE ... exiting"
exit
# fi
else
echo "## using existing osm file $OSM_FILE"
fi
}
function ensureMaven {
# maven home existent?
if [ "x$MAVEN_HOME" = "x" ]; then
# not existent but probably is maven in the path?
MAVEN_HOME=`mvn -v | grep "Maven home" | cut -d' ' -f3`
if [ "x$MAVEN_HOME" = "x" ]; then
# try to detect previous downloaded version
MAVEN_HOME="$GH_HOME/maven"
if [ ! -f "$MAVEN_HOME/bin/mvn" ]; then
echo "No Maven found in the PATH. Now downloading+installing it to $MAVEN_HOME"
cd "$GH_HOME"
MVN_PACKAGE=apache-maven-3.0.5
wget -O maven.zip http://www.eu.apache.org/dist/maven/maven-3/3.0.5/binaries/$MVN_PACKAGE-bin.zip
unzip maven.zip
mv $MVN_PACKAGE maven
rm maven.zip
fi
fi
fi
}
function packageCoreJar {
if [ ! -f "$JAR" ]; then
echo "## now building graphhopper jar: $JAR"
echo "## using maven at $MAVEN_HOME"
#mvn clean
"$MAVEN_HOME/bin/mvn" -f "$GH_HOME/core/pom.xml" -DskipTests=true install assembly:single > /tmp/graphhopper-compile.log
returncode=$?
if [[ $returncode != 0 ]] ; then
echo "## compilation failed"
cat /tmp/graphhopper-compile.log
exit $returncode
fi
else
echo "## existing jar found $JAR"
fi
}
function prepareEclipse {
ensureMaven
packageCoreJar
cp core/target/graphhopper-*-android.jar android/libs/
}
## now handle actions which do not take an OSM file
if [ "x$ACTION" = "xclean" ]; then
rm -rf */target
exit
elif [ "x$ACTION" = "xeclipse" ]; then
prepareEclipse
exit
elif [ "x$ACTION" = "xandroid" ]; then
prepareEclipse
"$MAVEN_HOME/bin/mvn" -f "$GH_HOME/android/pom.xml" install android:deploy android:run
exit
fi
if [ "x$FILE" = "x" ]; then
echo -e "no file specified? try \n$USAGE"
exit
fi
# NAME = file without extension if any
NAME="${FILE%.*}"
if [ ${FILE: -4} == ".osm" ]; then
OSM_FILE=$FILE
elif [ ${FILE: -4} == ".pbf" ]; then
OSM_FILE=$FILE
elif [ ${FILE: -7} == ".osm.gz" ]; then
OSM_FILE=$FILE
else
# no end default to osm
OSM_FILE=$NAME.osm
fi
GRAPH=$NAME-gh
VERSION=`grep "<name>" -A 1 pom.xml | grep version | cut -d'>' -f2 | cut -d'<' -f1`
JAR=core/target/graphhopper-$VERSION-jar-with-dependencies.jar
# file without path (.osm.gz or osm.bz2 is also possible)
TMP=$(basename "$FILE")
TMP="${TMP%.*}"
TMP="${TMP%.*}"
if [ "x$TMP" = "xunterfranken" ]; then
LINK="http://download.geofabrik.de/openstreetmap/europe/germany/bayern/unterfranken.osm.bz2"
JAVA_OPTS="-XX:PermSize=60m -XX:MaxPermSize=60m -Xmx200m -Xms200m"
elif [ "x$TMP" = "xgermany" ]; then
LINK=http://download.geofabrik.de/openstreetmap/europe/germany.osm.bz2
# Info: for import we need a more memory than for just loading it
JAVA_OPTS="-XX:PermSize=60m -XX:MaxPermSize=60m -Xmx1600m -Xms1600m"
else
LINK=`echo $NAME | tr '_' '/'`
if [ ${FILE: -4} == ".osm" ]; then
LINK="http://download.geofabrik.de/$LINK-latest.osm.bz2"
else
LINK="http://download.geofabrik.de/$LINK-latest.osm.pbf"
fi
if [ "x$JAVA_OPTS" = "x" ]; then
JAVA_OPTS="-XX:PermSize=60m -XX:MaxPermSize=60m -Xmx1000m -Xms1000m"
fi
fi
ensureOsmXml
ensureMaven
packageCoreJar
echo "## now $ACTION. JAVA_OPTS=$JAVA_OPTS"
if [ "x$ACTION" = "xui" ] || [ "x$ACTION" = "xweb" ]; then
export MAVEN_OPTS="$MAVEN_OPTS $JAVA_OPTS"
"$MAVEN_HOME/bin/mvn" -f "$GH_HOME/web/pom.xml" -Dgraphhopper.config=$CONFIG \
-Dgraphhopper.osmreader.osm=$OSM_FILE -Djetty.reload=manual jetty:run
elif [ "x$ACTION" = "ximport" ]; then
"$JAVA" $JAVA_OPTS -cp "$JAR" com.graphhopper.GraphHopper printVersion=true config=$CONFIG \
graph.location="$GRAPH" \
osmreader.osm="$OSM_FILE"
elif [ "x$ACTION" = "xtest" ]; then
"$JAVA" $JAVA_OPTS -cp "$JAR" com.graphhopper.GraphHopper printVersion=true config=$CONFIG \
graph.location="$GRAPH" osmreader.osm="$OSM_FILE" prepare.chShortcuts=false \
graph.testIT=true
elif [ "x$ACTION" = "xmeasurement" ]; then
ARGS="graph.location=$GRAPH osmreader.osm=$OSM_FILE prepare.chShortcuts=fastest osmreader.acceptWay=CAR"
echo -e "\ncreate graph via $ARGS, $JAR"
START=$(date +%s)
"$JAVA" $JAVA_OPTS -cp "$JAR" com.graphhopper.GraphHopper $ARGS prepare.doPrepare=false
END=$(date +%s)
IMPORT_TIME=$(($END - $START))000
function startMeasurement {
COUNT=5000
ARGS="$ARGS prepare.doPrepare=true measurement.count=$COUNT measurement.location=$M_FILE_NAME graph.importTime=$IMPORT_TIME"
echo -e "\nperform measurement via $ARGS, $JAR"
"$JAVA" $JAVA_OPTS -cp "$JAR" com.graphhopper.util.Measurement $ARGS
}
# use all <last_commits> versions starting from HEAD
last_commits=$3
if [ "x$last_commits" = "x" ]; then
# use current version
"$MAVEN_HOME/bin/mvn" -f "$GH_HOME/core/pom.xml" -DskipTests clean install assembly:single
startMeasurement
exit
fi
commits=$(git rev-list HEAD -n $last_commits)
for commit in $commits; do
git checkout $commit -q
M_FILE_NAME=`git log -n 1 --pretty=oneline | grep -o "\ .*" | tr " ,;" "_"`
M_FILE_NAME="measurement$M_FILE_NAME.properties"
echo -e "\nusing commit $commit and $M_FILE_NAME"
"$MAVEN_HOME/bin/mvn" -f "$GH_HOME/core/pom.xml" -DskipTests clean install assembly:single
startMeasurement
done
fi
[Expanded from my comment...] The script file apparently has DOS-style line endings (i.e. carriage return followed by linefeed, instead of just linefeed). This confuses the shell greatly, since it sees the carriage return as part of the command. The giveaway is that first error message:
: not found.sh: 2: graphhopper.sh:
What's actually happened is it printed the error message "graphhopper.sh: 2: graphhopper.sh: ^M: not found" (where the ^M is actually a carriage return); when the terminal sees the ^M it goes back to the beginning of the line, and prints the end of the error message over top of the beginning.
One of the other effects this has is that the shell can't recognize keywords at the end of lines. When it sees a line like:
if [ ${OSM_FILE: -4} == ".pbf" ]; then^M
...it thinks then^M a regular command, not the end of the condition part of the if command, so it keeps looking for a then. But the else command seems to have some spaces at the end:
else ^M
...which means the shell does recognize the else keyword and get very confused about what it's doing in the middle of the condition part of the if.
So what can you do about it? There's almost certainly a command for it; I'm used to dos2unix, but apparently ubuntu doesn't have that, instead the "tofrodos" package includes the command fromdos (see here). Or, you can do it with perl:
perl -pi -e 's/\r//g' graphhopper.sh
Your text editor may also be able to save in unix (rather than DOS) format. Speaking of which, you should either switch your text editor to unix mode, or find a different text editor for scripting.
Remove the extra fi:
if [ ! -f "$OSM_FILE" ]; then
echo "ERROR couldn't download or extract OSM file $OSM_FILE ... exiting"
exit
# fi
else
echo "## using existing osm file $OSM_FILE"
fi
The variable OSM_FILE is not set to anything. This causes it to expand to a empty string, which causes the shell to think there are syntax errors in the if conditions.
Print out the value of OSM_FILE before use, and if it's empty, debug backwards.

Resources