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
to parse arguments,
for fname
is sufficient[[
is a bash keyword similar to (but more powerful than) the[
command. See http://mywiki.wooledge.org/BashFAQ/031 and http://mywiki.wooledge.org/BashGuide/TestsAndConditionals. Unless you're writing for POSIX sh, I recommend[[
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[@]}"