Skip to content
Advertisement

if condition implementation in linux shell script

I have a list of 100+ folders in a linux at a folder path “files/data” where I already have basic script “list.sh”.

for d in files/data/*
do
echo $d
done

When I run it…

sh list.sh

It is listing all the 100+ folders (amazon, apple, cola, pepsi, and etc…) in side the “files/data” folder. But I am looking for some if command implementation to achieve the bellow.

When I call…

sh list.sh --brand=apple

Then it should only list “apple” and if i call…

sh list.sh --brand=all

Then it should list all the 100+ folders (amazon, apple, cola, pepsi, and etc…).

Thanks.

Advertisement

Answer

I’ve edit your’s script

Usage:

./script.sh all – show all dirs in folder path

./script.sh apple – show apple dirs

You can’t use script without parameter because usage of grep.

Hope that’s all what you need

#!/bin/bash

brand=$1


function ShowDirs()
{
for d in files/data/*
do
    if ! [[ $brand == all ]];
     then
        echo $d | grep $brand
    else
        echo $d
    fi
done
}

ShowDirs
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement