Home > Software engineering >  Linux Return code not working properly when running script
Linux Return code not working properly when running script

Time:01-23

I have the following script written in linux bash to check if file exists and I can read its contents. When run manually its working. But when I run the script, return code is blank.

${PARAM_FILES_ARR[@]} has values like /somedir/file1.prm, /somedir/files2.prm etc.

cdoe written below. Plesae help.

#!/bin/bash
for FNAME in "${PARAM_FILES_ARR[@]}"
    do
        cat "${FNAME}" > /dev/null 2>&1
        RETURN_CODE_FILE=$?
        if [ "${RETURN_CODE_FILE}" -ne 0 ]; then
            echo "Warning! Could not Read PARAM FILE ${FNAME}"
            echo "${RETURN_CODE_FILE}"
        fi
    done

CodePudding user response:

Simplify

for fname; do
    if [[ -r $fname ]]; then
        echo "readable"
    else
        echo "unreadable"
    fi
done

Usage

./script <*files> or <bash array>

To go deeper

CodePudding user response:

Your script don't see an array with files as Allan Wind suggested in comments. Try to change it like so:

#!/bin/bash
for FNAME in "$@"
    do
    ...

And run like this:

./script_name "${PARAM_FILES_ARR[@]}"
  • Related