2

Hello i am currently trying to make an if statement within shell script to detect if the user has no inputted any text.

i would like if the user hasnt entered anythign or if what they entered is invalid to echo and alert until they type in something correct.

here is what i have so far

echo "Current Address books "
        ls -R |grep .txt
        echo "--------------------------------------------------------------------"
        echo -n  "Please Enter the file you want to search in: "
        read fileName
        book=$fileName
        if [ $fileName == "" ]
        then
                echo "Please Enter some Text "
        else
                echo -n "Please Enter a name: "
        read search

        grep -i $search $fileName

        if grep -q "$search" "$fileName";
        then
                echo "Found!"
        else
                echo "Not Found!"
        fi

fi

3 Answers 3

1

Something like this?

#!/bin/bash

echo "Current Address books "
find -name '*.txt' -type f -exec bash -c 'printf "%s\n" "${@#./}"' _ {} + # Don't parse the output of ls
printf -v spacesep "%68s"; printf '%s\n' "${spacesep// /-}"
fileName=
while [[ -z $fileName ]]; do
    read -rep "Please Enter the file you want to search in: " fileName
    book=$fileName # Is this variable used?
    # Check that the file is really a file
    if [[ -n $fileName ]] && [[ ! -f $fileName ]]; then
      echo "Not a file, try again"
      fileName=
    fi
done
search=
while [[ -z $search ]]; do
    read -rep "Please Enter a name: " search
    grep -i -- "$search" "$fileName"
done
if grep -qi -- "$search" "$fileName"; then
   echo "Found!"
else
   echo "Not Found!"
fi

Bonus. With the -e switch to read you have readline editing capabilities, and tab completion on files. Wooow!

Sign up to request clarification or add additional context in comments.

3 Comments

can you explain what 'printf "%s\n" "${@#./}"' does to me?
@user258030 Without this, the output of find will have leading ./ in front of every file name. The snippet "${@#./}" expands to all positional parameters with leading ./ removed. So printf "%s\n" "${@#./}" prints, one per line, the positional parameters with leading ./ removed.
Thanks that was a complex yet perfect solution to what i needed
1

You can use -z "$var" notation:

if [ -z "$fileName" ]; then
   echo "Please Enter some Text "
else
   read -p "Please Enter a name: " search
fi

to check if string length is zero.

Alternatively you can do this check using quoted variable name:

if [ "$fileName" == "" ]; then
   echo "Please Enter some Text "
else
   read -p "Please Enter a name: " search
fi

Comments

1

You can use a control loop

until [  $fileName != "" ]; do
     echo "Please Enter some Text " ;
     read -p "Please Enter a name: " fileName ;
done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.