LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-18-2014, 01:30 AM   #1
VijayB
LQ Newbie
 
Registered: Jun 2014
Posts: 21

Rep: Reputation: Disabled
How do i print names with status


Hi,

I have a text file say test.txt. This test file has contents as :

Amit=2
Sahil=3
Vijay=1


i check the integer value in front of the names & print the status by following shell script:

#!/bin/bash
for i in $(cat ~/Precheck/test.txt | awk -F= '{print $2}')
do
if [ "$i" -eq 1 ]
then
echo " Guest has read access "
elif [ "$i" -eq 2 ]
then
echo " Guest has Read/Write access "
elif [ "$i" -eq 3 ]
then
echo " Guest has admin access "
else
echo "Guest does not have access"
fi
done

Output of this shell script is:

Guest has Read/Write access
Guest has admin access
Guest has read access


But the requirement is that instead of Guest , i should be able to print the names of the guests as well.

For example : Guest Amit has read/Write access & so on.

Can somebody please suggest how do i do that.

Thanks in Advance!

Vijay
 
Old 07-18-2014, 04:07 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,305

Rep: Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816
Ok, so what have you tried so far?
 
Old 07-18-2014, 08:39 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
First of all, please place code or data inside [code][/code] tags so it is readable and legible.

Next I would suggest you look at how to read from a file using a while loop in bash and at the field separator IFS (also used in bash)
 
Old 07-18-2014, 09:25 AM   #4
VijayB
LQ Newbie
 
Registered: Jun 2014
Posts: 21

Original Poster
Rep: Reputation: Disabled
@TenTenths : i tried cascaded for loop but it didn't work. Can you suggest something else?
 
Old 07-18-2014, 09:35 AM   #5
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,305

Rep: Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816
Make use of awk and consider using case instead of if/elif.

I could easily give you an elegant solution if you want it, but that stops you learning about how to do stuff yourself.

For example, this is my output using your input file:

Code:
# ./level.sh test.txt
Amit has Read/Write access
Sahil has admin
Vijay has read access
 
Old 07-19-2014, 03:40 AM   #6
VijayB
LQ Newbie
 
Registered: Jun 2014
Posts: 21

Original Poster
Rep: Reputation: Disabled
Hi TenTenths,

Thanks for boosting me up.

I am able to define just one variable & names i have defind inside case statement. can you please help me out , how can i map names with values.
The code that i am using is as follows:

Code:
#!/bin/bash


cat ~/Precheck/test.txt | awk -F= '{print $2}'| while read i ; do
	
case $i
in

1) 
Name=Vijay
echo -e "Guest $Name has read access" ;;

2)
Name=Amit
echo -e "Guest $Name has read/Write access" ;;

3)
Name=Sahil
echo -e "Guest $Name has admin access" ;;
 
esac
done
Thanks in Advance!

Vijay
 
Old 07-19-2014, 04:02 AM   #7
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,305

Rep: Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816
Script level.sh
Code:
#!/bin/bash

for LINE in $(cat $1) ; do

  USER=$(echo ${LINE} | awk -F= {'print $1'})
  ACCESS=$(echo ${LINE} | awk -F= {'print $2'})

  case ${ACCESS} in

    1)
    echo "${USER} has read access"
    ;;

    2)
    echo "${USER} has Read/Write access"
    ;;

    3)
    echo "${USER} has admin"
    ;;

    *)
    echo "${USER} does not have access."
    ;;

  esac

done
Use: ./level.sh ~/Precheck/test.txt
 
Old 07-19-2014, 11:25 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
Code:
while IFS='=' read -r user access
do
    case "$access" in
      1) echo "$user has read access";;
      2) echo "$user has Read/Write access";;
      3) echo "$user has admin"
      *) echo "$user does not have access."
    esac
done<"$1"
Call it with:
Code:
./access.sh input_file
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Bash question -- Narrow output of 'debootstrap --print-debs' to package names only Kenny_Strawn Linux - General 4 01-22-2011 08:58 PM
How to read print status file in embedded linux chiragshah Linux - Newbie 1 12-31-2008 01:16 AM
how to print function names & parmaters each time control enters the function? tanniru Linux - Networking 1 09-11-2008 02:21 AM
Printer fails to print: foomatic-rip stopped with status 1! robbyx Linux - Hardware 1 12-15-2006 04:38 AM
Force grep NOT to print file names smart_sagittari Linux - Newbie 5 04-25-2005 02:20 AM


All times are GMT -5. The time now is 10:56 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration