My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

Is it bad to use the conditions for `find`ing the directories and to `cd` multiple specific folders of a found directory to call the function?

Gustavo Benedito Costa's photo
Gustavo Benedito Costa
·Jan 6, 2019

I would like to find an icons theme somewhere to cd and then to replace and add/insert in all SVG files in multiple specific folders. I am not sure if my bash/shell codes are bad and inelegant in your eyes.

  1. Firstly begins the function go_dir_to_breeze. You can see the conditions in the end of code, in the which it will locate the existent icons theme somewhere. If it is found, you will enter in the function go_dir_to_breeze and begin the item (2).
  2. After found successfully, you are entering in the function change_colour(), which will begin the sed to replace and string, calling foo.pl, but instead, it will hear to the variable of the chosen colour/gradient in the conditions before beginning to call change_colour(). If the colour/gradient is found, it will begin to call foo.pl to replace and insert.
  3. The terminal will change the directory with cd, which will call the function change_colour().

For example:

echo "Which type of colour do you prefer? Write the nummber"
echo "1) solid 2) gradient"
read type

if [$type = "1"]; then
    echo "Write one of these colours to choose your favourite:"
    echo "black gold green\n grey orange purple\n red yellow"
    read color
else
    echo "Write one of these letters to choose your favourite gradient:"
    echo "a) telinkrin b) arrogrin c) minoan"
    read color
fi

go_dir_to_breeze()
{

    change_colour()
    {
        for f in *svg; do
               case "$color" in
            a)
                perl gradient1.pl "$f" > tmpFile && mv tmpFile "$f"
                ;;
            b)
                perl gradient2.pl "$f" > tmpFile && mv tmpFile "$f"
                ;;
            c)
                perl gradient3.pl "$f" > tmpFile && mv tmpFile "$f"
                ;;
            black)
                perl black.pl "$f" > tmpFile && mv tmpFile "$f"
                ;;
            gold)
                perl gold.pl "$f" > tmpFile && mv tmpFile "$f"
                ;;
            green)
                perl green.pl "$f" > tmpFile && mv tmpFile "$f"
                ;;
            grey)
                perl grey.pl "$f" > tmpFile && mv tmpFile "$f"
                ;;
            orange)
                perl orangle.pl "$f" > tmpFile && mv tmpFile "$f"
                ;;
            purple)
                perl purple.pl "$f" > tmpFile && mv tmpFile "$f"
                ;;
            red)
                perl red.pl "$f" > tmpFile && mv tmpFile "$f"
                ;;
        esac
        done
    }

    cd apps='apps/16'
    change_colour
    cd ..

    cd places='places/16'
    change_colour
    cd ..

    cd mimetypes='mimetypes/16'
    change_colour

}

if [ find /home/$USER/.icons/breeze ]; then
    go_dir_to_breeze
elif [ find /home/$USER/local/share/icons/breeze ]; then
    go_dir_to_breeze
# Observe that to change the colour of icons, it is required to run as root
elif [ find /usr/share/icons/breeze ]; then
    go_dir_to_breeze
else
    echo "Icons theme Breeze does not exist!"
fi

Are the conditions of finding the directories of icons theme good and bad? And is cding multiple specific folders to call the function change_colour good or bad?

So maybe the user can receive the errors in these cases.