Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
#!/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
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"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.