1

without using finger command ,This command will display the login name, full name, home directory, and default shell for each of the names given on the command line. If one of the names is not found it should display an error message. If its is entered without any arguments, then the above information should be displayed for all the users logged in to the system at that time. But i don't know how to check if the name is on the system or not !

here's my script so far for (user name) :

read command 

if [ "$command" = " myfinger " ]
   echo myfinger name
   read name
   x=$( cat /etc/passwd | grep $userId  | cut -d: -f1  )
   y=$( cat /etc/passwd | grep $userId  | cut -d: -f5 | cut -d_ -f1 )
   z=$( cat /etc/passwd | grep $userId  | cut -d: -f6  )
   w=$( cat /etc/passwd | grep $userId  | cut -d: -f7  )

   echo login name : $x
   echo full name : $y
   echo home dir : $z
   echo shell : $w
  if ( "$name" != " cat /etc/passwd | grep $userId  | cut -d: -f5 | cut -d_ -f1 " )
     then 
echo  error : not found

  fi
 if ( "$name" = null )

     cat /etc/passwd  | cut -d: -f5 | cut -d_ -f1
fi
fi

Vivian
  • 11
  • 4
  • 2
    Super sidenote: check your notes (because this looks like homework) if you're supposed to know about `getent`, because `/etc/passwd` will in some circumstances (like centralized credentials in LDAP) not list all users. – Ulrich Schwarz Dec 21 '19 at 19:26
  • @Jesse_b In fact, I do not know what is the point of not helping whether it is homework or not I mean if you don't understand something you will ask for help I can't understand what the site for if some people keep thinking in that way – Vivian Dec 21 '19 at 19:34
  • @Vivian: I'm not sure why that was in response to me. There is a dissonance between users on this site and homework questions due to how many poorly written ones we have seen that essentially just ask us to do all the work (See your last question), however this question shows that you have tried to solve the problem and includes something resembling function code so IMO it is a good question regardless of whether or not it is homework. – jesse_b Dec 21 '19 at 19:38
  • 1
    *"for each of the names given on the command line"* it sounds like the intent of the assignment is for you to structure your code as a script (or function) that loops over its *positional parameters*, rather than using `read` to interact with the user. See for example [Positional Parameters](http://linuxcommand.org/lc3_wss0120.php) – steeldriver Dec 21 '19 at 19:41
  • Please try to write a more focused question, using clear and unambiguous language. You say “I don't know how to check if the name is on the system …” When I first read that I thought you meant «I don’t know how to check whether a given user name is a *valid* user name; i.e., the name of a *valid* user, ***defined*** on this system.» (For example, `root`, `bin` and `sys` are valid names on most systems; `vivian` is a valid user on your system and not on mine; `gman` vice versa, etc.) … (Cont’d) – G-Man Says 'Reinstate Monica' Dec 21 '19 at 23:14
  • (Cont’d) … But then I saw that you know about `/etc/passwd` and `grep` (which, BTW, is ***a*** way to do that test, although not necessarily the only way or even the best way). So do you mean «I don’t know how to check whether a given user *is **currently logged in*** on this system»? I’m confused, because, based on your description of the problem, you don’t need to know that. … … … … … … … … … “Help?” is not an acceptable question. What do you need help with? (As Jesse_b says, we won’t do your homework assignment for you.) … (Cont’d) – G-Man Says 'Reinstate Monica' Dec 21 '19 at 23:14
  • (Cont’d) …  P.S. (1) Why are you testing the value of `$command` against a string with leading and trailing spaces? (2) Please use meaningful variable names. (`command` and `name` are not terrible. `x`, `y`, `z` and `w` *are* terrible.) (3) `userId` is a pretty good variable name, but your code uses ``$userId`` without ever setting it. … (Cont’d) – G-Man Says 'Reinstate Monica' Dec 21 '19 at 23:17
  • (Cont’d) …  (4) Test for errors from all commands (see [Copying code from Stack Exchange](https://stackoverflow.blog/2019/11/26/copying-code-from-stack-overflow-you-might-be-spreading-security-vulnerabilities "Copying code from Stack Overflow?  You might paste security vulnerabilities, too.")), especially the ones that you expect to fail (e.g., `grep gman /etc/passwd` will fail on your system).  (5) If at all possible, post code that at least runs.  The code you posted will fail essentially immediately because the first `if` doesn’t have a `then` (and neither does the third).  … (Cont’d) – G-Man Says 'Reinstate Monica' Dec 21 '19 at 23:17
  • (Cont’d) …  The fact that the second and third `if` statements use parentheses where they should (maybe) use square brackets is a big red flag.  (6) When you post code, please indent it correctly.  (7) Why are you comparing the value of `$name` to `null`?  The only way `$name` will be `null` is if the person running your script types `n` `u` `l` `l` in response to the `read name` command.  (8) Etc…. – G-Man Says 'Reinstate Monica' Dec 21 '19 at 23:17

0 Answers0