Shell Script To Check String Length (Display Message If It Doent't Have At Least 5 Characters )

in Categories Decision Making, String Management last updated June 28, 2008

Write a shell script that accepts a string from the terminal and echo a suitable message if it doesn’t have at least 5 characters including the other symbols. Script must not use array or array length to find out string length.

#!/bin/bash
# Shell script that accepts a string from the user and echo a suitable message if 
# it doesn't have at least 5 characters. 
# -------------------------------------------------------------------------
# Copyright (c) 2007 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
echo -n "Enter a string : "
read string
 
LEN=$(echo ${#string})
 
if [ $LEN -lt 5 ]; then
        echo "$string doesn't have at least 5 characters"
else
        echo "$string has 5 or more characters"
fi

Share this on:

2 comment

    Have a question? Post it on our forum!