Jump to content

Excluding Enter Key / Home Directory BASH


Recommended Posts

Quick Description

Writing script which prompts user for directory in which a number of txt files are stored.

The script then parses them for a particular heading and writes names of those files to another record file.

 

The Script actually works Hooray!

 

One Small problem: If you don't enter a directory and just hit Enter then you are in your home directory.

The script recognises this as valid and continues.

 

I'd like to stop this from happening if I can. I've tried a number of solutions

 

like

 

read $DIRECTORY

if [ -d $DIRECTORY ] && [ $DIRECTORY != $HOME ]

continue...

 

but none of these work

not even if you type /home/whatever $HOME expands to for the read $DIRECTORY

 

Any ideas?

 

Can I stop the script from continuing if someone just hits enter at this point?

Link to comment
Share on other sites

#!/bin/bash

while true
 do	
 while true 
   do
   echo "Enter a directory:"
   read DIRECTORY
   if [ -d $DIRECTORY ] && [ ! -z $DIRECTORY ]
   then
      break
          else
             echo "Try again:"
             continue
       fi
   done

<<do your stuff with your directory info here>>

  echo "That was great. Do you wish to do it again (Y/n)?"
 read ANSWER
 if [[ $ANSWER == "y" || $ANSWER == "Y" || $ANSWER == "" ]] 
    then
    continue
 else
    break
 fi
 done

 echo "We are done."

 

I'm sure aru can do much better.

Edited by Steve Scrimpshire
Link to comment
Share on other sites

Maybe it is your syntax, because I saw you do something like this:

read $DIRECTORY

  if [ -d $DIRECTORY ] && [ ! -z $DIRECTORY ]

 

when it should be just:

read DIRECTORY

  if [ -d $DIRECTORY ] && [ ! -z $DIRECTORY ]

 

This script works fine for me. What exactly do you have now?

Link to comment
Share on other sites

Hi Steve,

 

$DIRECTORY was a typo I didn't paste from the original script.

 

I have now got this to work based on the information you gave me this morning. (Friday)

 

The if clause wasn't really the BIG problem.

 

I had a following clause which evaluated to true - even if the string was null :oops:

This was an until loop

which asked the user to read DIRECTORY until the directory existed.

Basically hit the Enter key and you had satisfied the condition - heh presto your in your HOME directory.

 

Had to power down all my machines at work to day at 3.45pm or I'd have posted earlier.

 

Solution is in work (Dublin) While I'm currently at home in Belfast.

 

However I can certainly post it on Monday if the example is of any use to anyone?

 

Seems like good manners to make this offer even if it's not a particularly sophisticated

script.

 

Once again Steve many thanks. Much appreciated.

Link to comment
Share on other sites

Solution is in work (Dublin) While I'm currently at home in Belfast.

 

However I can certainly post it on Monday if the example is of any use to anyone?

 

Seems like good manners to make this offer even if it's not a particularly sophisticated

script.

 

Once again Steve many thanks. Much appreciated.

 

I'm sure the working copy of this script would benefit others and be more than welcome.

Link to comment
Share on other sites

Background: I had a large number of files to check for the presence of a particular legal clause.

It's been renamed Bongo. The files are all in different languages but luckily the legal clause refers to a particular named product which always appears in English so in a way I was using grep to search for a keyword.

 

Round here a couple of days was scheduled to open and scan through every file to look for the title!

Then a list of files containing the clause and those not containing the clause was to be compiled. You gotta love proprietry software :)

 

Anyway I didn't need a script but there are a lot of people round here are using Linux but are a bit command-line shy. The sript was written to work for them and to demonstrate for the lug just how powerful the various commands can be. It's my first attempt at a script for anything usefull.

 

It's not finished, A lot of it is still hard coded -I'm trying to make it a bit more portable and a bit more useful and perhaps even a bit more elegant. But for the moment it seems to do what it was intended to do.

 

So for what it's worth here it is:

 

#!/bin/bash

DEFRESULTS=search_results

echo -e "\nThis script will find the word \"Bongo\" in"
echo -e  "any text file in which it appears.\n"

echo -e "Enter the path to the directory where licence files are stored."
echo -e "If directory is in $HOME the directory name will be enough:\n"

read DRCTRY


#check directory exists
if  [ -n "$DRCTRY" ] && [ -d "$DRCTRY" ]
	 then 
	 echo Directory exists
 else	
	 echo Directory does not exist
	 echo Continue Y / n ?
	 read CONT
	 if [ "$CONT" = "y" ] || [ "$CONT" = "Y" ] 
   then
  	 until [ -n "$DRCTRY" ] && [ -d "$DRCTRY" ]
     do
    	 echo Enter the directory where licence files are stored:
    	 read DRCTRY
     done
  	 echo Directory Exists!
   else
  	 exit 0
	 fi
   
 fi

   cd $DRCTRY
   pwd

   
echo -e "Enter the name of output file or results will be written to ~/$DEFRESULTS"
read RESULTS

if [ -z $RESULTS ]
then 
RESULTS=$DEFRESULTS
fi


 
	 #echo used first with > to create or overwrite output file.
	 echo -e "The Bongo Clause appears in the following files:\n" > ~/$RESULTS
	 #Search for Bongo -l filename instead line including text.
	 grep -l '\<Bongo\>' * | tee -a ~/$RESULTS
	 #count $RESULTS and assign to NUMY
	 NUMY=`grep -l '\<Bongo\>' * | wc -l`
	 
	 #write to standard output and a file
	 #use echo as tee is not getting input from a file or command
	 echo -e "\n$NUMY Files contain the Bongo Clause\n\n\n" | tee -a ~/$RESULTS
	 
	 echo -e "list files which do not contain \"Bongo\" y/n"
	 
	 read CONT1
 
 #If y continue to list files not containing "Bongo"
 #Anything else and exit
 
 
 if [ "$CONT1" = "y" ] || [ "$CONT1" = "Y" ]
	 then
   echo -e "The Bongo Clause absent from the following files:\n" >> ~/$RESULTS
   grep -L '\<Bongo\>' * | tee -a ~/$RESULTS
   NUMN=`grep -L '\<Bongo\>' * | wc -l` 
   echo Results writen to ~/$RESULTS
   echo -e "\n $NUMN Files do not contain the Bongo Clause\n"  | tee -a ~/$RESULTS
	 else
   echo Results writen to ~/$RESULTS
 fi
 
 

exit 0

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...