About

Script Categories

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

Posted in Decision Making » String Management

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

Download - Email this to a friend - Printable version

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tags: , , , , , , , ~ Last updated on: June 28, 2008