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
# 1  
Old 03-07-2011
Question How to find text in files without using the word itself but the assigned variable of it

I'm having a problem how to find the specific word in a file without using the word itself as a search but using the assigned variable which is the $passwd.. what command should I use to find the value of $passwd written in different script? how do I use the command to print the value in this script??

Also, how can I compare a value written in this script to the value written in different script using only the assigned variable which is $username?? thank you.

My use of grep $username userfile | tr ':' ' ' is really ineffective..

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
  clear
  tput cup 2 4; echo "      Password Recovery      "
  tput cup 3 4; echo "============================="
  tput cup 4 4; echo "Username: " ; read user 
     if ("$user" = grep $username userfile | tr ':' ' ');     #username is the username of the user when he created an account, it is stored in login_users
         then 
              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 12 8; echo "choice : "         
         else
              tput cup 14 8; echo "Invalid choice. Choose another."
     fi         
              read question || continue
              case $question in
                   [Aa]) tput cup  8; echo "answer in question A: " ; read $A
                      if  ("$answer"= grep $A userfile | tr ':' ' ')  #A is the answer of the user in question A, it is stored in the login_users 
                        then      
                             echo "Correct, your password is: " 
                             echo 
                             grep $pass $userfile | tr ':' ' '   #pass is the original password of the user, it is stored in login_users
                        else
                             echo "Incorrect. Choose another question to answer." ;
                      fi ;;

Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 03-07-2011 at 02:58 PM.. Reason: code tags, please!
# 2  
Old 03-07-2011
Please show an example of your datafile.
It is very herd to visualize what you are referring to.
# 3  
Old 03-07-2011
Here is the sample output,,

Password Recovery
=============================
Username: (for example I enter "jeni")
./forgotpass=. command not found

Then choose a question to answer below
(A) What is your 5th favorite color? (I have many choices but I'll just include 1)
choice: a

Answer in A: (for example I answered gold and its correct)

(then it will say)

Correct. Your password is:

(then it will appear blank. it will not show the password. it is assigned $passwd in different script. "login_users" is my script in which the $username, $passwd, and correct answers to questions are stored) Please help me. thanks.
# 4  
Old 03-07-2011
Seems this part has issue:

Code:
if ("$user" = grep $username userfile | tr ':' ' ');

change to:

Code:
u=$(grep $username userfile | tr ':' ' ' )

if [[  "$user" == "$u" ]];
then
   Commands.
fi

# 5  
Old 03-07-2011
Umm similar problem here too:

Code:
 if  ("$answer"= grep $A userfile | tr ':' ' ')

change to:
Code:
 if [[ "$answer" == $(grep $A userfile | tr ':' ' ') ]]

Code:
[Aa]) tput cup  8; echo "answer in question A: " ; read $A

The above should probably be read A not read $A


In relation to this:
Code:
echo "Correct, your password is: " 
echo 
grep $pass $userfile | tr ':' ' '   #pass is the original password of the user, it is stored in login_users

Can't see where $pass is set in this script. You many need to post an example of how data is stored in the login_users file, because I think this grep won't find the correct line.

Another thought: be carefull that users cant directly read ~/casestudies/login_users otherwise they can get password without using your script (can use sudo to run script as another user that can read the login_users file).

Last edited by Chubler_XL; 03-07-2011 at 08:19 PM..
# 6  
Old 03-08-2011
@rdcwayx

when I used your correction to my script, it already went to the Invalid code.. blah blah.. then it terminated..But the username I entered is really in the login_users script..
what is the problem in this??

@chubler XL

That's what I'm thinking about.. but how can I do the sudo script??

These are the scripts related to the password recovery..

Here is the script to add accounts in the login_users

Code:
#============================================================
#  Script Name:   useradd
#  By:            JRG
#  Date:          March 5, 2011
#  Purpose:       A shell script used to add 
#          new user accounts to the login_users file
#  Command Line:  useradd
#===========================================================
trap "rm ~/tmp/* 2> /dev/null; exit" 0 1 2 3
userfile=~/casestudies/login_users

  clear
  tput cup 1 4; echo "User Accounts Addition"
  tput cup 2 4; echo "======================="
  tput cup 4 4; echo "Username : "
  tput cup 5 4; echo "Password : "
  tput cup 6 4; echo "Answer the following questions in case of password recovery"
  tput cup 7 2; echo "A. What is your 5th favorite color? "
  tput cup 8 2; echo "B. What is your favorite food? "
  tput cup 9 2; echo "C. What is the name of your pet? "
  tput cup 10 2; echo "D. What is the middle name of your mother's maiden name? "
  tput cup 11 2; echo "E. What is the model number of your laptop/monitor? "
  
  tput cup 4 18; read username
  tput cup 5 18; read passwd
  tput cup 7 18; read A
  tput cup 8 18; read B
  tput cup 9 18; read C
  tput cup 10 18; read D
  tput cup 11 18; read E    
# Check to see if username is not blank before you
#write to disk
  if [ "$username" > "           " ] 
  then echo "$username:$passwd:$A:$B:$C:$D:$E" >> $userfile 
  fi
  tput cup 13 10; echo "(q)uit: "
  tput cup 13 10; read choice
  if [ "$choice" = "q" ]
    then 
      clear; exit   
  fi

Printing of lists
Code:
#============================================================
#  Script Name:   userlist
#  By:            JRG
#  Date:          March 5, 2011
#  Purpose:       Use awk to format colon-separated fields
#          in a flat file and display to the screen
#  Command Line:  userlist
#===========================================================
clear
  tput cup 2 4; echo "User List"
  tput cup 3 4; echo "========="
  tput cup 5 0; echo "P - Print User List"
  awk -F: '{printf "%-12s %-12s %s\t%s %s %10.10s %s\n", $2, $3, $4, $1, $5, $6, $7}' login_users

To select which one to do..
Code:
#============================================================
#  Script Name:   profile
#  By:            JRG
#  Date:          March 5, 2011
#  Purpose:       main menu 
#  Command Line:  profile
#===========================================================
profile=~/casestudies/users
loop=y
while [ "$loop" = y ]
do
  clear
  tput cup 3 12; echo "  Welcome to your profile  "
  tput cup 4 12; echo "==========================="
  tput cup 6 9; echo "What do you want to do? "
  tput cup 7 9; echo "C - create an account"
  tput cup 8 9; echo "L - Login"
  tput cup 9 9; echo "F - Forgot password"
  tput cup 10 9; echo "Q - Quit"
   read choice || continue
     case $choice in
       [Cc]) ./useradd;;
       [Ll]) ./login1 ;;
       [Ff]) ./forgotpass;;
       [Qq]) exit ;;
        *) tput cup 13 4; echo "Invalid Code"; read choice ;;
     esac
done

# 7  
Old 03-08-2011
hi jenimesh Smilie
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