LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 11-22-2014, 07:41 AM   #1
niko420
LQ Newbie
 
Registered: Nov 2014
Posts: 3

Rep: Reputation: Disabled
compare id and password from predefined one file using shell script linux..


i am trying to write shell script
how to write shell script which is ask for user id and password then compare both from one file..
 
Old 11-22-2014, 04:39 PM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,153

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
Post what you have already tried we are not going to do your homework for you!
 
Old 11-25-2014, 12:27 PM   #3
niko420
LQ Newbie
 
Registered: Nov 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
i have only idea to compare of two elements

echo "enter first number"
read first
echo "enter second number"
read second

if [ $first -eq $second ]
then
echo "Both Values are equal"
i have no idea how to compare with numbers present in file.... please help me on this how to do this..

---------- Post added 11-25-14 at 01:28 PM ----------

Quote:
Originally Posted by Keith Hedger View Post
Post what you have already tried we are not going to do your homework for you!


i have only idea to compare of two elements

echo "enter first number"
read first
echo "enter second number"
read second

if [ $first -eq $second ]
then
echo "Both Values are equal"
i have no idea how to compare with numbers present in file.... please help me on this how to do this..
 
Old 11-25-2014, 01:09 PM   #4
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,153

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
First off use code blocks as it makes your code easier to understand like so
Code:
some
code goes
here
This would be a better start point
Code:
#!/bin/bash -e

read -p "User ID: " first
read -p "Password: " second

if [ X$first = X$second ];then
	echo "Both Values are equal"
else
	echo "Mismatch"
fi
Always start your scripts with the relevant #!
You must terminate an if/then/else block with fi.
In the text above the use of the 'X' added to the strings prevent errors at run time as both X's cancel, this way even if one string is undefined ( the user may have just hit 'return' without entering a value ) the script wont fall over.
doing an echo and then read is wasteful just use the -p option to read to set a prompt.
Using a '=' allows any sort of input '1234', 'abcd' 11a22b3c' etc, the '-eq' compares numbers only and will exit with an error if it encounters a non number.

Get a copy of the 'advanced bash scripting guide' which contrary to what the name says covers the basics of bash scripting as well as advanced topics.
I hope this will give you a start, try to do a bit more on your own and report back if you get stuck.
 
Old 11-25-2014, 01:14 PM   #5
niko420
LQ Newbie
 
Registered: Nov 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
but i am confuse that how to compare this string present in file..
 
Old 11-25-2014, 01:40 PM   #6
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,153

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
The script will be dependent on the format of the file you are comparing you input'ed values against
As I said grab a copy of abs and TRY, then post you results this is really quite basic stuff if you put a bit of effort into it.
 
Old 11-26-2014, 11:48 AM   #7
pascaltaf
LQ Newbie
 
Registered: Nov 2014
Location: Paris suburb
Distribution: LFS, Debian
Posts: 26

Rep: Reputation: Disabled
just some tips

If i understand well, you would like to compare the string you read from the user to a string in a file.
So what you need is commands to read a file, right ?
If so, you should be interested by commands like :
cat, grep, sed, cut, tail, head ...
For example, if I want to get the encrypted password of a user in /etc/shadow (if I'm root), I can do something like :

grep '^my_username:' /etc/shadow | cut -d : -f 2

(I'm sorry I don't know the technique to put code blocks in my post).
Basically, grep '^my_username:' /etc/shadow will display all the lines of /etc/shadow beginning by my_username: (normally not more than one), and cut -d : -f 2 will modify these lines by displaying on a subpart, which is the part just after the first : and before the second :.
You should try "man grep", "man cut" ... to learn more about these commands.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about Bash Shell script to compare file sizes michaellopez12 Linux - Server 10 07-23-2014 04:43 PM
compare file size shell script arrals.vl Programming 4 03-07-2013 11:29 PM
[SOLVED] How to compare string 15.05 with float 15.0 in Linux shell script kverma1985 Programming 3 09-06-2012 02:30 AM
Shell Script Compare Folders corteplaneta Programming 8 09-18-2010 01:32 AM
shell script: compare 2 files anhtt Programming 6 08-29-2007 02:39 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 11:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration