• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Solving case sensitive problem for xml files in Linux

danielvasc

Member
Joined
Apr 19, 2014
Messages
5
Reaction score
7
I was facing some issues on my Ubuntu 22.04 server because the .lua files mentioned in my actions.xml file were saved with both uppercase and lowercase letters, and Linux is case-sensitive. As a result, I encountered such problems:

Code:
>> Loading vocations
>> Loading items... OTB v2.7.2
>> Loading script systems
>> Using Lua 5.2
>> Loading lua libs
[Warning - Event::checkScript] Can not load script: scripts/Quest/Ab'Dendriel/Draconia/draconiaQuestWalls.lua
cannot open data/actions/scripts/Quest/Ab'Dendriel/Draconia/draconiaQuestWalls.lua: No such file or directory
[Warning - Event::checkScript] Can not load script: scripts/Quest/Ankrahmun/green djinn/tear.lua
cannot open data/actions/scripts/Quest/Ankrahmun/green djinn/tear.lua: No such file or directory
[Warning - Event::checkScript] Can not load script: scripts/Quest/Ankrahmun/hot cuisine/cookbook.lua
cannot open data/actions/scripts/Quest/Ankrahmun/hot cuisine/cookbook.lua: No such file or directory
[Warning - Event::checkScript] Can not load script: scripts/Quest/Ankrahmun/hot cuisine/cookbook1.lua
cannot open data/actions/scripts/Quest/Ankrahmun/hot cuisine/cookbook1.lua: No such file or directory
[Warning - Event::checkScript] Can not load script: scripts/Quest/Ankrahmun/Steal From Thieves/stealFromThieves.lua
cannot open data/actions/scripts/Quest/Ankrahmun/Steal From Thieves/stealFromThieves.lua: No such file or directory

You can imagine the headache it would be to rename each file manually, right?

So, I created a shell script to solve my problem:

Bash:
#!/bin/bash


# Function to extract the script path from XML lines
get_script_path() {
    local line=$1
    local start_pos=$(expr index "$line" '"')
    local end_pos=$(expr index "${line:$start_pos}" '"')
    echo "${line:$start_pos:$end_pos-1}"
}


# Main script starts here
# Check if the XML file is provided as argument
if [ $# -ne 1 ]; then
    echo "Usage: $0 <actions.xml>"
    exit 1
fi


# Store the XML file path
xml_file=$1


# Check if the XML file exists
if [ ! -f "$xml_file" ]; then
    echo "Error: XML file '$xml_file' not found."
    exit 1
fi


# Loop through each line in the XML file
while read -r line; do
    # Check if the line contains the 'script=' attribute
    if [[ "$line" == *"script=\""* ]]; then
        # Extract the script path from the line
        script_path=$(get_script_path "$line")
        
        # Check if the script file exists
        if [ ! -f "/home/TFS-1.5-Downgrades-8.0/data/actions/scripts/$script_path" ]; then
            echo "Script file '$script_path' not found."
        else
            # Extract the directory and filename from the script path
            dir_path=$(dirname "$script_path")
            file_name=$(basename "$script_path")
            
            # Change directory to the script directory
            cd "/home/TFS-1.5-Downgrades-8.0/data/actions/scripts/$dir_path" || exit
            
            # Rename the script file to match the filename in the XML
            mv "$(ls | grep -i "$file_name")" "$file_name"
            
            # Output success message
            echo "Script file '$script_path' renamed successfully."
        fi
    fi
done < "$xml_file"

This script is designed to read an XML file containing a list of script paths and their corresponding filenames. It iterates through each line of the XML file, extracts the script path, and checks if the corresponding script file exists in a specific directory. If the script file exists, it renames the script file to match the filename specified in the XML. Finally, it outputs a success message for each script file that is successfully renamed.

To run the script, simply navigate to the "data/actions" directory and execute the following command:
Bash:
./rename_scripts.sh actions.xml

Remember that to execute the script, you need to make it executable. To do this in Linux, simply use the following command:
Bash:
chmod +x rename_scripts.sh

You can adapt it to any situation by simply modifying some lines in the script where the directory is defined, as in my example: "/home/TFS-1.5-Downgrades-8.0/data/actions/scripts/$script_path".
 
Back
Top