473,473 Members | 2,286 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

problem with if statement

8 New Member
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
1 2226
Luuk
1,047 Recognized Expert Top Contributor
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
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.