How to find text in files without using the word itself but the assigned variable of it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to find text in files without using the word itself but the assigned variable of it
# 8  
Old 03-08-2011
Forgetting about security for the moment, the following should work for you:

Code:
#============================================================
#  Script Name:   forgotpass
#  By:            JRG
#  Date:          March 5, 2011
#  Purpose:       A shell script that enables a user to 
#          enter username and recover password
#  Command Line:  forgotpass
#===========================================================
userfile=~/casestudies/login_users
while true
do
  clear
  tput cup 2 4; echo "      Password Recovery      "
  tput cup 3 4; echo "============================="
  tput cup 4 4; printf "Username: $user"
  [[ -z "$user" ]] && read user
  [[ -z "$user" ]] && break
 
  # $user is the username of the user when he created an account,
  # now fetch info on this user from the login_users file into $line
  line=$(grep "^$user:" $userfile)
 
  if [[ -z "$line" ]]
  then
      tput cup 17 6; printf "Invalid username. Enter another."
      user=
      sleep 2
      continue
  fi
 
  tput cup  6 4; echo "Then choose a question to answer below:"
  tput cup  7 8; echo "(A) What is your 5th favorite color? "
  tput cup  8 8; echo "(B) What is your favorite food? "
  tput cup  9 8; echo "(C) What is the name of your pet? "
  tput cup 10 8; echo "(D) What is the middle name of your mother's maiden name? "
  tput cup 11 8; echo "(E) What is the model number of your laptop/monitor? "
  tput cup 13 8; echo "(Q) Quit"
  tput cup 15 6; printf "choice : " ; read question
  case $question in
      [AaBbCcDd])
          tput cup 17 6; printf "Answer in question $question: "
          read answer
          FLD=$(( 0x$question - 7 ))
          if [[ "$answer" = "$(echo $line | cut -d: -f$FLD)" ]]
          then
              printf "Correct, your password is: "
              echo $line | cut -d: -f2
              printf "\nPress enter when done."
              read q
              break
          else
              tput cup 18 6
              printf "Incorrect. Choose another question to answer."
          fi
      ;;
      [Qq])
          break
      ;;
      *)
          tput cup 17 6; printf "Invalid choice. Choose another."
      ;;
  esac
  sleep 2
done

How it works:

We grep the user from login_users file and assign to $line, it's a : separated list with 6 fields 1=user 2=pass 3-6 answers to questions A thru D

To reduce code we prompt for the question and answer for all 4 questions with the same code. The user selects the question and we end up with a value of A thru D in the variable question.
This needs to be converted to a field number from 3 to 6 so we do:

FLD=$(( 0x$question - 7 )) This treats the value of question as a hex number then subtracts 7 so A->3; B->4 C->5 and D->6. Now we can use $FLD with cut to fetch the answer from the line variable.

Last edited by Chubler_XL; 03-08-2011 at 07:01 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find a word and increment the number in the word & save into new files

Hi All, I am looking for a perl/awk/sed command to auto-increment the numbers line in file, P1.tcl: run_build_model sparc_ifu_dec run_drc set_faults -model path_delay -atpg_effectiveness -fault_coverage add_delay_paths P1 set_atpg -abort_limit 1000 run_atpg -ndetects 1000 I would like... (6 Replies)
Discussion started by: jypark22
6 Replies

2. Shell Programming and Scripting

Find command to find a word from list of files

I need to find a word '% Retail by State' in the folder /usr/sas/reports/RetailSalesTaxallocation. When I tried like below, -bash-4.1$ cd /usr/sas/reports/RetailSalesTaxallocation -bash-4.1$ find ./ -name % Retail by State find: paths must precede expression: Retail Usage: find ... (10 Replies)
Discussion started by: Ram Kumar_BE
10 Replies

3. Shell Programming and Scripting

How to find the number of occurence of particular word from a text file?

example: i have the following text file... i am very tired. i am busy i am hungry i have to find the number of occurence of a particular word 'am' from the text file.. can any one give the shell script for it (34 Replies)
Discussion started by: sheela
34 Replies

4. UNIX for Dummies Questions & Answers

Find EXACT word in files, just the word: no prefix, no suffix, no 'similar', just the word

I have a file that has the words I want to find in other files (but lets say I just want to find my words in a single file). Those words are IDs, so if my word is ZZZ4, outputs like aaZZZ4, ZZZ4bb, aaZZZ4bb, ZZ4, ZZZ, ZyZ4, ZZZ4.8 (or anything like that) WON'T BE USEFUL. I need the whole word... (6 Replies)
Discussion started by: chicchan
6 Replies

5. UNIX for Dummies Questions & Answers

How do you check if a variable has been assigned?

I am trying to check whether a variable has been assigned on the command line or not. Here is what I did: #!/usr/bin/bash if( $variable == '\0') { print "variable was not assigned" exit } else NF = 2 {print $1, ""} exit fi awk -f question1.awk variable = 58 letters.txt. So... (3 Replies)
Discussion started by: Fred63528
3 Replies

6. Shell Programming and Scripting

Find and replace a word in all the files (that contain the word) under a directory

Hi Everyone, I am looking for a simple way for replacing all the files under a directory that use the server "xsgd1234dap" with "xsdr3423pap". For Example: In the Directory, $pwd /home/nick $ grep -l "xsgd1234dap" *.sh | wc -l 119 I have "119" files that are still using... (5 Replies)
Discussion started by: filter
5 Replies

7. Solaris

Command files when the output is assigned to a variable

Hi, i'm posting this in the Solaris forum although maybe it should be better in the General unix forum, I'm formatting an output witht he following command: crontab -l | grep GBOUAT8 | grep UTP | grep -i stop | sed 's/\\//' 08 2 * * 2-6 /apps/sum_glob/gbo_uat/sparse/bin/dmg_cronlaunch -ENVI... (2 Replies)
Discussion started by: Cvg
2 Replies

8. Shell Programming and Scripting

How to find and print the last word of each line from a text file

Can any one help us in finding the the last word of each line from a text file and print it. eg: 1st --> aaa bbbb cccc dddd eeee ffff ee 2nd --> aab ered er fdf ere ww ww f the o/p should be a below. ee f (1 Reply)
Discussion started by: naveen_sangam
1 Replies

9. Shell Programming and Scripting

Check if a variable has a value assigned?

Hi, I want to check if a variable has a value assigned to it or not. I can do following - cat $Var > File1 if then echo "$Var has value" else echo "$Var is null" fi But I have to check for 3 Variables and I want to wrap it up in couple of unix statements. Any... (3 Replies)
Discussion started by: sumeet
3 Replies

10. UNIX for Dummies Questions & Answers

What does $? mean when assigned to a variable?

If i write this statement in a Korn Shell script RCODE=$? what possibly does it eman? (3 Replies)
Discussion started by: ranjita.c
3 Replies
Login or Register to Ask a Question