Tuesday, January 12, 2016

Shell script to display all the files which start from a specific letter? For e.g., if letter = s then all files started with letter s should be displayed from the current directory.

# letter.sh
# Program :         To display all the files which start from a specific letter? For e.g., if letter = s then all files started with letter s should be displayed from the current directory. 
# Author :           sujith
# Date :              17/03/2015

echo "Enter the letter"
read letter

flag=0
len=$(echo "$letter" | wc -c)

if [ $len -eq 2 ]
then
        echo "The names of files starting with $len"
        for str in `ls`
        do
                c=$(echo "$str" | cut -c1)
                if [ $c == $letter ]
                then
                        if [ -f $str ]
                        then
                                flag=1
                                echo $str
                        fi
                fi
        done
        if [ $flag -eq 0 ]
        then
                echo "No files which start with the letter $letter"
        fi
else
        echo "Enter only one letter"
fi


Output:
[141740@localhost ~]$ sh letter.sh
Enter the letter
s
The names of files starting with 2
salary.c
sample
sample.c
samplex.c
search.sh
she116.sh
shel16.sh
shell10.sh
shell11.sh
shell12.sh
shell13.sh
shell1.sh
shell2.sh
shell3.sh
shell4.sh
shell5.sh
shell6.sh
shell7.sh
shell8.sh
shell9.sh
sin.c
sinloop.c
sqaure1.c
sqaure.c
square1.c
square.c
stack.cpp
std.c
strcmp.c
strconcat.c
string.l
strlen.c
strpal.sh
str.sh
structure.cpp
sum.c

[141740@localhost ~]$

[141740@localhost ~]$ sh letter.sh
Enter the letter
z
The names of files starting with 2
No files which start with the letter z


[141740@localhost ~]$

1 comment:

  1. 1xbet korean: How to use your bonus in Korea
    1xbet 1xbet apk korean Betway bonus code, 1xbet korean Betway bonuses, 1xbet korean Betway bonus code, 1xbet korean Betway promotions.

    ReplyDelete