0

Firstly , In the /tmp/test file path I have the below directories

amb
bmb
cmb

By running the find command, I am getting the below list of files inside these three directories:

amb/eng/canon.amb
bmb/eng/case.bmb
cmb/eng/hint.cmb

I am trying to take each file from this list1 using a for loop and based on the file type, *.amb or *.bmb or *.cmb that particulal IF should execute:

cd /tmp/test
find */ -type f -exec ls {} \; > /tmp/test/list1
for file in `cat /tmp/test/list1`
do
if [ -f *.amb ]
then
sed "s/amb/amx/g" /tmp/test/list1 > /tmp/test/list2
ls /tmp/test/list2 >> /tmp/test/finallist
fi

if [ -f *.bmb ]
then
sed "s/bmb/bmx/g" /tmp/test/list1 > /tmp/test/list2
ls /tmp/test/list2 >> /tmp/test/finallist
fi

if [ -f *.cmb ]
then
sed "s/cmb/cmx/g" /tmp/test/list1 > /tmp/test/list2
ls /tmp/test/list2 >> /tmp/test/finallist
fi

done
echo "*********************"
echo -e "\nFinal list of files after replacing from tmp area"
felist=`cat /tmp/test/finallist`

echo -e "\nfefiles_list=`echo $felist`"

So my final output should be:

amx/eng/canon.amx
bmx/eng/case.bmx
cmx/eng/hint.cmx
0

2 Answers 2

2

I think you're trying to apply a different action depending on the file suffix:

#!/bin/bash
while IFS= read -d '' -r file
do
    # amb/eng/canon.amb
    extn=${file##*.}

    case "$extn" in
    (amb)   finallist+=("${file//amb/amx}") ;;
    (bmb)   finallist+=("${file//bmb/bmx}") ;;
    (cmb)   finallist+=("${file//cmb/bmx}") ;;
    esac
done <( cd /tmp/test && find */ -type f -print0 2>/dev/null )

printf '*********************\n\n'
printf 'Final list of files after replacing from tmp area\nfefiles_list=%s\n' "${finallist[*]}"

Incidentally,

  • find */ -type f -exec ls {} \; > /tmp/test/list1 could better be written as find */ -type f -print > /tmp/test/list1, and since you've tagged with even that than be reduced further to find */ -type f > /tmp/test/list1. But this will break on strange (but legal) filenames.
  • Backticks are deprecated and you should be using $( … ) instead. But even that will break with filenames containing spaces and other special characters.
4
  • script getting failed at this step done <( cd /tmp/test && find */ -type f -print0 2>/dev/null ) as No such file or directory i tried giving cat instead of cd to the file path , even though it is throwing same error
    – afrin
    Commented Jun 16, 2023 at 12:43
  • @afrin your own code starts with cd /tmp/test so I've used that as my starting point too Commented Jun 16, 2023 at 13:23
  • Could you please suggest, to get rid of this
    – afrin
    Commented Jun 19, 2023 at 5:23
  • @afrin no I can't. You have provided the information and requirements. I don't know your situation so if you say it shows that "X is true" then I work with the information that X is true. Commented Jun 19, 2023 at 7:14
2

Use else if, aka elif in bash, e.g.:

if [ $something ]; then
    echo "something"
elif [ $something_else ]; then
    echo "something_else"
elif [ $nothing ]; then
    echo "nothing"
else
    echo "no match"
fi

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.