Score:0

I keep getting an end of file error. I tried to check the code manually and I tried multiple online bash checkers, can anyone help?

mn flag

I wrote this bash script to deploy a web app automatically to my tomcat server. I keep getting an unexpected end of file error but the code seems fine to me. I also tried multiple online bash syntax checkers and they didn't find any problems.

I'm using an Ubuntu 20.04 LTS Hyper-V VM

Here's the bash code:

#!/bin/bash
CURRENT_DIRECTORY_PATH="$PWD"
PROPS_PATH=$CURRENT_DIRECTORY_PATH/iiq_auto_deployment.properties
certsIIQ_PATH=$CURRENT_DIRECTORY_PATH/certsIIQ
TOMCAT_PATH=$(grep TOMCAT_PATH "$PROPS_PATH" | cut -d= -f2)
WAR_PATH=$(grep WAR_PATH "$PROPS_PATH" | cut -d= -f2)
SERVICE_PATH=$(grep SERVICE_PATH "$PROPS_PATH" | cut -d= -f2)
BACKUP_PATH=$(grep BACKUP_PATH "$PROPS_PATH" | cut -d= -f2)
BACKUP_IIQ=$(grep BACKUP_IIQ "$PROPS_PATH" | cut -d= -f2)
BACKUP_WAR=$(grep BACKUP_WAR "$PROPS_PATH" | cut -d= -f2)
launchIIQConsole=$(grep launchIIQConsole "$PROPS_PATH" | cut -d= -f2)
date=$(date +%d-%m-%Y)
FILECMD_IMPORT=$CURRENT_DIRECTORY_PATH/import_sp_init_custom.txt
IIQPROPERTIES_FILE=/home/centos/Desktop #a enlever
# Check if the passenc file already exists
if [ ! -f "$certsIIQ_PATH/.passenc" ]
then
    # Read user's username and password
    read -r -s -p "Username: " username
    read -r -s -p "Password: " password

    # Generate private key and public key to encrypt/decrypt password
    mkdir "$certsIIQ_PATH"
    sudo openssl genrsa -out "$certsIIQ_PATH"/passwordPrivKey.pem 2048
    sudo openssl rsa -in "$certsIIQ_PATH"/passwordPrivKey.pem -text -noout
    sudo openssl rsa -in "$certsIIQ_PATH"/passwordPrivKey.pem -pubout -out "$certsIIQ_PATH"/passwordPubKey.pem
    sudo openssl rsa -in "$certsIIQ_PATH"/passwordPubKey.pem -pubin -text -noout
    sudo chmod 400 "$certsIIQ_PATH"/passwordPrivKey.pem


    # Save username and password inside a file and encrypt it
    echo "$password" > "$certsIIQ_PATH"/.pass
    echo "$username" > "$certsIIQ_PATH"/.username
    sudo chmod 400 "$certsIIQ_PATH"/.pass
    sudo openssl rsautl -encrypt -inkey "$certsIIQ_PATH"/passwordPubKey.pem -pubin -in "$certsIIQ_PATH"/.pass -out "$certsIIQ_PATH"/.passenc
    rm -f "$certsIIQ_PATH"/.pass    
fi
if [ ! -d "$BACKUP_PATH" ]
then
    mkdir "$BACKUP_PATH"
fi
if [ ! -d "$BACKUP_IIQ" ]
then
    mkdir "$BACKUP_IIQ"
fi
if [ ! -d "$BACKUP_WAR" ]
then
    mkdir "$BACKUP_WAR"
fi
if [ -d "$TOMCAT_PATH" ]
then
    if [ -f "$WAR_PATH" ]
    then
        if [ -f "$SERVICE_PATH" ]
        then
            sudo systemctl stop tomcat
            echo "Le service Apache Tomcat s'est arrete correctement."
            sleep 1.5
            echo "Sauvagarde du dossier IdentityIQ actuel..."
            sleep 1
            sudo mv "$TOMCAT_PATH"/webapps/identityiq "$BACKUP_IIQ"/identityiq.backup"$date"
            echo "Decompression du fichier WAR..."
            sleep 1.5
            sudo unzip -o "$WAR_PATH" -d "$TOMCAT_PATH"/webapps/identityiq
            echo "Decompression du fichier WAR terminee."
            sudo cp $IIQPROPERTIES_FILE/iiq.properties "$TOMCAT_PATH"/webapps/identityiq/WEB-INF/classes #a enlever
            if [ -f "$TOMCAT_PATH/webapps/identityiq/WEB-INF/lib/mssql-jdbc-10.2.1.jre17.jar" ]
            then
                sudo rm "$TOMCAT_PATH"/webapps/identityiq/WEB-INF/lib/mssql-jdbc-10.2.1.jre17.jar 
            fi
            if [ "$launchIIQConsole" == true ]
            then
                if [ ! -f "$FILECMD_IMPORT" ]
                then
                    echo "Creation du fichier temporaire..."
                    echo "import sp.init-custom.xml">"$FILECMD_IMPORT"
                fi
                username=$(cat "$certsIIQ_PATH"/.username)
                password=$(sudo openssl rsautl -decrypt -inkey"$certsIIQ_PATH"/passwordPrivKey.pem -in "$certsIIQ_PATH"/.passenc)
                sudo chmod +x "$TOMCAT_PATH"/webapps/identityiq/WEB-INF/bin/iiq
                echo "IIQ Console is launched!"
                cd "$TOMCAT_PATH"/webapps/identityiq/WEB-INF/bin/ && ./iiq console -u "$username" -p "$password" < "$FILECMD_IMPORT"
            fi
            sudo systemctl start tomcat
            echo "Le service Apache Tomcat a demarre correctement."
            sudo mv "$WAR_PATH" "$BACKUP_WAR"/identityiq.war"$date"
            if [ -f "$FILECMD_IMPORT" ] 
            then
                rm "$FILECMD_IMPORT" 
            fi
            sudo find "$BACKUP_IIQ" -mtime +1 -exec rm -rf {} \;
            sudo find "$BACKUP_WAR" -mtime +1 -exec rm -rf {} \;
            echo "Old files deleted successfully."
        else
            echo "Le fichier $SERVICE_PATH n'existe pas."
        fi
    else
        echo "Le fichier $WAR_PATH n'existe pas."
    fi
else
    echo "Le repertoire $TOMCAT_PATH n'existe pas."
fi

If anyone can help I'd really appreciate it. Thanks!

hr flag
It might help to include the complete, exact error message in your question (it should indicate a line number)
uz flag
Jos
I tested the script with `scriptcheck` (from within PHPStorm), and it didn't find any errors. In fact, I was able to execute it (without any meaningful result, obviously).
Hannu avatar
ca flag
"unexpected end of file error " usually has a "on line xxx". attached to it. Check that line or the line before it thoroughly (most often it is a parenthese or quote-problem) .
hr flag
... it could be as simple as a stray non-printing character (such as a carriage return making `fi` into `fi\r`) that is not apparent when you paste the script into an online syntax checker. Check the line endings with something like `cat -e` or `cat -A`
Hady Ijreiss avatar
mn flag
Thanks for the help everyone, in the end what fixed the problem was the command: `sed -i -e 's/\r$//' iiq_auto_deployment.sh` I guess because I copied the script from my Windows to my Linux VM there was a problem with how the return to line \r\n vs \n
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.