Skip to main content
added 2 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

For a literal interpretation of your issue: Call some other script if at least one of the tree filenames CV_REF_DATA_09012021.txt, DB_ONLINE_CL_09012021.txt, DFR_CL_INS_09012021.txt exists in the directory /NAS/CFG. Add the existing names to an array.

shopt -s nullglob
unset -v names

for name in CV_REF_DATA_09012021.txt DB_ONLINE_CL_09012021.txt DFR_CL_INS_09012021.txt
do
        [ -e /NAS/CFG/"$name" ] && names+=( "$name" )
done

if [ "${#names[@]}" -eq 0 ]; then
        echo 'no files could be found' >&2
        exit 1
fi

# At least one name is in the "names" array.
# Call your other script below here.

For a literal interpretation of your issue: Call some other script if at least one of the tree filenames CV_REF_DATA_09012021.txt, DB_ONLINE_CL_09012021.txt, DFR_CL_INS_09012021.txt exists in the directory /NAS/CFG. Add the existing names to an array.

shopt -s nullglob
unset -v names

for name in CV_REF_DATA_09012021.txt DB_ONLINE_CL_09012021.txt DFR_CL_INS_09012021.txt
do
        [ -e /NAS/CFG/"$name" ] && names+=( "$name" )
done

if [ "${#names[@]}" -eq 0 ]; then
        echo 'no files could be found' >&2
        exit 1
fi

# At least one name is in the "names" array.
# Call your other script below here.
added 2 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

Your description of what you want to happen, depending on the existence of the files, is a bit vague. It's also unclear if you wish to collect the names separately for each filename prefix or together. It may also be better to pick a particular date, for example, and try to find the set of files from that. The code below should provide a bit of support for your programming effort.

This answer will be similar to my response to your previous question in that we will loop over a set of static strings to test whether filenames containing those strings exists or not.

You may loop over the filename prefixes, and in each iteration, check whether any file with that prefix (and with the .txt filename suffix) exists.

for prefix in CV_REF_DATA_ DB_ONLINE_CL_ DFR_CL_INS_; do
        set -- /NAS/CFG/"$prefix"*.txt
        if [ -e "$1" ]; then
                printf 'There exists names matching "%s"*.txt:\n' "$prefix"
                printf '\t%s\n' "$@"
        else
                printf 'No names matches "%s"*.txt\n' "$prefix"
        fi
done

The loop iterates over the filename prefixes, and in each iteration, we try to expand the pattern that should match the files we are interested in for that prefix. The set command will store the matching filenames in the list of positional parameters ("$@"; "$1" is the first element of that list).

In the bash shell, we may write the above block of code as follows.

shopt -s nullglob

for prefix in CV_REF_DATA_ DB_ONLINE_CL_ DFR_CL_INS_; do
        names=( /NAS/CFG/"$prefix"*.txt )
        if [ "${#names[@]}" -gt 0 ]; then
                printf 'There exists names matching "%s"*.txt:\n' "$prefix"
                printf '\t%s\n' "${names[@]}"
        else
                printf 'No names matches "%s"*.txt\n' "$prefix"
        fi
done

The nullglob shell option makes non-matching globbing patterns disappear rather than remain unexpanded. We use a named array, names, to hold any matching filenames in each iteration.

This answer will be similar to my response to your previous question in that we will loop over a set of static strings to test whether filenames containing those strings exists or not.

You may loop over the filename prefixes, and in each iteration, check whether any file with that prefix (and with the .txt filename suffix) exists.

for prefix in CV_REF_DATA_ DB_ONLINE_CL_ DFR_CL_INS_; do
        set -- /NAS/CFG/"$prefix"*.txt
        if [ -e "$1" ]; then
                printf 'There exists names matching "%s"*.txt:\n' "$prefix"
                printf '\t%s\n' "$@"
        else
                printf 'No names matches "%s"*.txt\n' "$prefix"
        fi
done

The loop iterates over the filename prefixes, and in each iteration, we try to expand the pattern that should match the files we are interested in for that prefix. The set command will store the matching filenames in the list of positional parameters ("$@"; "$1" is the first element of that list).

In the bash shell, we may write the above block of code as follows.

shopt -s nullglob

for prefix in CV_REF_DATA_ DB_ONLINE_CL_ DFR_CL_INS_; do
        names=( /NAS/CFG/"$prefix"*.txt )
        if [ "${#names[@]}" -gt 0 ]; then
                printf 'There exists names matching "%s"*.txt:\n' "$prefix"
                printf '\t%s\n' "${names[@]}"
        else
                printf 'No names matches "%s"*.txt\n' "$prefix"
        fi
done

The nullglob shell option makes non-matching globbing patterns disappear rather than remain unexpanded. We use a named array, names, to hold any matching filenames in each iteration.

Your description of what you want to happen, depending on the existence of the files, is a bit vague. It's also unclear if you wish to collect the names separately for each filename prefix or together. It may also be better to pick a particular date, for example, and try to find the set of files from that. The code below should provide a bit of support for your programming effort.

This answer will be similar to my response to your previous question in that we will loop over a set of static strings to test whether filenames containing those strings exists or not.

You may loop over the filename prefixes, and in each iteration, check whether any file with that prefix (and with the .txt filename suffix) exists.

for prefix in CV_REF_DATA_ DB_ONLINE_CL_ DFR_CL_INS_; do
        set -- /NAS/CFG/"$prefix"*.txt
        if [ -e "$1" ]; then
                printf 'There exists names matching "%s"*.txt:\n' "$prefix"
                printf '\t%s\n' "$@"
        else
                printf 'No names matches "%s"*.txt\n' "$prefix"
        fi
done

The loop iterates over the filename prefixes, and in each iteration, we try to expand the pattern that should match the files we are interested in for that prefix. The set command will store the matching filenames in the list of positional parameters ("$@"; "$1" is the first element of that list).

In the bash shell, we may write the above block of code as follows.

shopt -s nullglob

for prefix in CV_REF_DATA_ DB_ONLINE_CL_ DFR_CL_INS_; do
        names=( /NAS/CFG/"$prefix"*.txt )
        if [ "${#names[@]}" -gt 0 ]; then
                printf 'There exists names matching "%s"*.txt:\n' "$prefix"
                printf '\t%s\n' "${names[@]}"
        else
                printf 'No names matches "%s"*.txt\n' "$prefix"
        fi
done

The nullglob shell option makes non-matching globbing patterns disappear rather than remain unexpanded. We use a named array, names, to hold any matching filenames in each iteration.

added 2 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

This answer will be similar to my response to your previous question in that we will loop over a set of static strings to test whether filenames containing those strings exists or not.

You may loop over the filename prefixes, and in each iteration, check whether any file with that prefix (and with the .txt filename suffix) exists.

for prefix in CV_REF_DATA_ DB_ONLINE_CL_ DFR_CL_INS_; do
        set -- /NAS/CFG/"$prefix"*.txt
        if [ -e "$1" ]; then
                printf 'There exists names matching "%s"*.txt:\n' "$prefix"
                printf '\t%s\n' "$@"
        else
                printf 'No names matches "%s"*.txt\n' "$prefix"
        fi
done

The loop iterates over the filename prefixes, and in each iteration, we try to expand the pattern that should match the files we are interested in for that prefix. The set command will store the matching filenames in the list of positional parameters ("$@"; "$1" is the first element of that list).

In the bash shell, we may write the above block of code as follows.

shopt -s nullglob

for prefix in CV_REF_DATA_ DB_ONLINE_CL_ DFR_CL_INS_; do
        names=( /NAS/CFG/"$prefix"*.txt )
        if [ "${#names[@]}" -gt 0 ]; then
                printf 'There exists names matching "%s"*.txt:\n' "$prefix"
                printf '\t%s\n' "${names[@]}"
        else
                printf 'No names matches "%s"*.txt\n' "$prefix"
        fi
done

The nullglob shell option makes non-matching globbing patterns disappear rather than remain unexpanded. We use a named array, namesnames, to hold any matching filenames in each iteration.

This answer will be similar to my response to your previous question.

You may loop over the filename prefixes, and in each iteration, check whether any file with that prefix (and with the .txt filename suffix) exists.

for prefix in CV_REF_DATA_ DB_ONLINE_CL_ DFR_CL_INS_; do
        set -- /NAS/CFG/"$prefix"*.txt
        if [ -e "$1" ]; then
                printf 'There exists names matching "%s"*.txt:\n' "$prefix"
                printf '\t%s\n' "$@"
        else
                printf 'No names matches "%s"*.txt\n' "$prefix"
        fi
done

The loop iterates over the filename prefixes, and in each iteration, we try to expand the pattern that should match the files we are interested in for that prefix. The set command will store the matching filenames in the list of positional parameters ("$@"; "$1" is the first element of that list).

In the bash shell, we may write the above block of code as follows.

shopt -s nullglob

for prefix in CV_REF_DATA_ DB_ONLINE_CL_ DFR_CL_INS_; do
        names=( /NAS/CFG/"$prefix"*.txt )
        if [ "${#names[@]}" -gt 0 ]; then
                printf 'There exists names matching "%s"*.txt:\n' "$prefix"
                printf '\t%s\n' "${names[@]}"
        else
                printf 'No names matches "%s"*.txt\n' "$prefix"
        fi
done

The nullglob shell option makes non-matching globbing patterns disappear rather than remain unexpanded. We use a named array, names, to hold any matching filenames in each iteration.

This answer will be similar to my response to your previous question in that we will loop over a set of static strings to test whether filenames containing those strings exists or not.

You may loop over the filename prefixes, and in each iteration, check whether any file with that prefix (and with the .txt filename suffix) exists.

for prefix in CV_REF_DATA_ DB_ONLINE_CL_ DFR_CL_INS_; do
        set -- /NAS/CFG/"$prefix"*.txt
        if [ -e "$1" ]; then
                printf 'There exists names matching "%s"*.txt:\n' "$prefix"
                printf '\t%s\n' "$@"
        else
                printf 'No names matches "%s"*.txt\n' "$prefix"
        fi
done

The loop iterates over the filename prefixes, and in each iteration, we try to expand the pattern that should match the files we are interested in for that prefix. The set command will store the matching filenames in the list of positional parameters ("$@"; "$1" is the first element of that list).

In the bash shell, we may write the above block of code as follows.

shopt -s nullglob

for prefix in CV_REF_DATA_ DB_ONLINE_CL_ DFR_CL_INS_; do
        names=( /NAS/CFG/"$prefix"*.txt )
        if [ "${#names[@]}" -gt 0 ]; then
                printf 'There exists names matching "%s"*.txt:\n' "$prefix"
                printf '\t%s\n' "${names[@]}"
        else
                printf 'No names matches "%s"*.txt\n' "$prefix"
        fi
done

The nullglob shell option makes non-matching globbing patterns disappear rather than remain unexpanded. We use a named array, names, to hold any matching filenames in each iteration.

Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
Loading