473,408 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

problem with if statement

8
Hi! When one types a user that doesnt exist it immediately goes to the else statement instead of into the if statement and prints user doesnt exist how do i change this? This is my function:

function viewUserProperties {
echo "Which user would you like to view?"
read user

if [[ $user == 0 ]];
then
echo "User does not exist!"
else
printf "\nUser view for $user\n"
printf "\n-15s\n %-15s\n %-15s\n %-15s\n
%-15s\n%-15s\n%-15s\n", "Username" "Password" "User ID" "Group ID" "Directory" "Shell"
getent passwd $user | tr ':' '\n'

fi
}


Output now:
view for arnold

Username
Password
User ID
Group ID
Directory
Shell
arnold
x
1003
1003
/home/arnold
/bin/bash

I also wish to change the output into table format as seen below. How do i do that? Thanks in advance!

view for arnold

Username arnold
Password x
User ID 1003
Group ID 1003
Directory /home/arnold
Shell /bin/bash
Dec 30 '18 #1

✓ answered by Luuk

You are not checking if your user exists, or not!
Also if you do not enter a username, $user will be '', and $user will only be 0 if you type-in '0'.

I rewrote your code to this:
Expand|Select|Wrap|Line Numbers
  1. #!/bin/bash
  2. #set -x
  3. read user
  4. getentUser=$(getent passwd $user)
  5. if [[ $? != 0 ]];
  6. then
  7.                 echo "User does not exist!"
  8. else
  9.                 printf "\nUser view for $user\n"
  10.  
  11.                 (
  12.                 echo "Username:Password:User ID:Group ID:Directory:Shell"
  13.                 echo "$getentUser"
  14.                 ) | gawk -F: 'NR==1{ for (i=1; i<=6; i++) { a[i]=$i }}
  15.                 NR==2{ for (i=1; i<=6; i++) { b[i]=$i }}
  16.                 END{ for (i=1; i<=6; i++){ printf "%-15s: %-15s\n", a[i], b[i]}}'
  17. fi
  18.  
on line#4 the existance if the user is tested
line#5 if the returnvalue of getent is not equal to 0, than $user does not exist
lines#11-#13 create the output on 2 lines, with fields separated by ':
lines#14-#16 use GAWK to display a table

if you remove the '#' in line#2, you will see every line before it gets evaluated.

1 2224
Luuk
1,047 Expert 1GB
You are not checking if your user exists, or not!
Also if you do not enter a username, $user will be '', and $user will only be 0 if you type-in '0'.

I rewrote your code to this:
Expand|Select|Wrap|Line Numbers
  1. #!/bin/bash
  2. #set -x
  3. read user
  4. getentUser=$(getent passwd $user)
  5. if [[ $? != 0 ]];
  6. then
  7.                 echo "User does not exist!"
  8. else
  9.                 printf "\nUser view for $user\n"
  10.  
  11.                 (
  12.                 echo "Username:Password:User ID:Group ID:Directory:Shell"
  13.                 echo "$getentUser"
  14.                 ) | gawk -F: 'NR==1{ for (i=1; i<=6; i++) { a[i]=$i }}
  15.                 NR==2{ for (i=1; i<=6; i++) { b[i]=$i }}
  16.                 END{ for (i=1; i<=6; i++){ printf "%-15s: %-15s\n", a[i], b[i]}}'
  17. fi
  18.  
on line#4 the existance if the user is tested
line#5 if the returnvalue of getent is not equal to 0, than $user does not exist
lines#11-#13 create the output on 2 lines, with fields separated by ':
lines#14-#16 use GAWK to display a table

if you remove the '#' in line#2, you will see every line before it gets evaluated.
Jan 1 '19 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Jole | last post by:
Hi I'm using serialization to read in an object from a file. My code basically does this: 1)MyClass obj = (MyClass) in.readObject(); ('in' is an ObjectInputStream wrapped around a...
3
by: Robert Mark Bram | last post by:
Hi All! I have the following two methods in an asp/jscript page - my problem is that without the update statement there is no error, but with the update statement I get the following error: ...
3
by: Andrew Bonello | last post by:
Hi I have written a C++ program (Redhat Linux 7.2, gcc v2.96). The program makes a sequence of calls to rand() after having seeded the random number generator using srand( 0 ). Under normal...
15
by: Andrew Maclean | last post by:
I guess this problem can be distilled down to: How do I search through a string, find the first matching substring, replace it, and continue through the string doing this. Can replace_if() be used...
10
by: Bryce Calhoun | last post by:
Hello, First of all, this is a .NET 1.1 component I'm creating. SUMMARY ----------------------- This component that I'm creating is, for all intents and purposes, a document parser (I'm...
11
by: Gagan | last post by:
//Problem Statement // //You work for a company that sells mechanical bit counting devices. These devices //wear out when they do a lot of counting. You are going to see how much wear has...
2
by: badrbadr | last post by:
Hi, Today, I find a problem from an old Google Code Jam Contest. You can read the problem statement here : http://www3.sympatico.ca/red.zrari/problem.htm I solved the problem in 15 min (wow, c#...
14
by: dl | last post by:
I have two classes, say A and B, both having a data member 'int n'; private in A, public in B. When I derive class C from both public A and public B, B::n should be visible to C while A::n...
2
by: Terry Reedy | last post by:
SUBHABRATA, I recommend you study this excellent response carefully. castironpi wrote: It starts with a concrete test case -- an 'executable problem statement'. To me, this is cleared and...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.