1

I have a bash script that searchs for password for unrar files. I would like to concatenate result and at the end of the script notify result of execution, but I don´t know why final_result var outputs "INIT-END".

Why does not concatenate in search_pass_and_unrar function?

#!/bin/bash

# Url for finding rar files
url_hdd="/media/HDD"
final_result="INIT-"

unrar_files(){

    filebase=`dirname "$1"`
    url_rar="$1"
    url_for_pass=""

    # Search files for password
    find "$filebase" -name '*CONTR*.txt' | while read LINE; do

        # Convert Windows line ending
        $(sed -i 's/^M//g' "$LINE")

        # Get url where we can find file password
        url_for_pass=$(cat "$LINE" | grep -Eo '(http|https)://[^?"]+')

        search_pass_and_unrar "$url_for_pass" "$url_rar" "$filebase"

    done

}

search_pass_and_unrar(){

    final_url="$1"
    pass=$(curl -s -S -L "$final_url" | grep 'name="txt_password"' | grep -oP 'value="\K[^"]+')

    if [[ -z "$pass" ]]
    then
        final_result+="Error, password not found"
        return
    fi

    result_unrar=$(unrar e "${2}" "${3}" -p"${pass}")
    final_result+="Result: ${result_unrar}"


}

# Find rar files and call unrar_files function
find "$url_hdd" -type f -name "*.rar" | while read LINE; do
    unrar_files "$LINE"
done

final_result+="END"
echo "$final_result" # "INIT-END"

Thanks a lot, best regards.

1 Answer 1

1

Problem is here:

# Find rar files and call unrar_files function
find "$url_hdd" -type f -name "*.rar" | while read LINE; do
    unrar_files "$LINE"
done

Due to pipeline used here your script is forking another subshell and calling unrar_files in a subshell. Due to this subshell creation all the changes made to final_result are not visible in current shell.

You can fix it by using process substitution like this:

# Find rar files and call unrar_files function
while IFS= read -d '' -r LINE; do
    unrar_files "$LINE"
done < <(find "$url_hdd" -type f -name '*.rar' -print0)

Note use of -print0 to make sure we can process files with special characters as well.

Similarly inside unrar_files you need to this:

while IFS= read -d '' -r LINE; do

    # Convert Windows line ending
    $(sed -i 's/^M//g' "$LINE")

    # Get url where we can find file password
    url_for_pass=$(cat "$LINE" | grep -Eo '(http|https)://[^?"]+')

    search_pass_and_unrar "$url_for_pass" "$url_rar" "$filebase"

done < <(find "$filebase" -name '*CONTR*.txt' -print0)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer. I don´t know if there is a typo, I copied your code and the result is the same.
Ah you have another find "$filebase" -name '*CONTR*.txt' | while read LINE; do inside unrar_files that needs to be changed similarly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.