0

I am trying to write a script which for any name given as an argument prints the list of paths to home directories of people with the name. Example:

$ shownames jakub anna 
/home/users/jakubo 
/home/students/j_luczka 
/home/students/kubeusz 
/home/students/jakub5z  
/home/students/qwertinx 
/home/users/lazinska 
/home/students/annalaz

I am using Git Bash with files and directories on my local computer

This is what I wrote so far:

for iter in "$@"; do find "$(pwd)"/home -type d -name "*$iter*"; done;

And as expected I get the full path.

$ ./shownames.sh jakub
/c/Users/User/students/home/users/jakubo

But what if I want to get only the /home/...... part? without the full c/Users.... so on part?

Thanks a lot in advance!

iuridiniz
  • 2,213
  • 24
  • 26
  • Does the first sample show what you want? Can you put a sample of what you get? I didn't get the part 'C:/Users'..., are you using WSL? Can you show the result of $(pwd) (just type `pwd`) ? – iuridiniz Aug 07 '21 at 11:36
  • I am using Git Bash with my own files and directories on my local computer. This is the result of pwd: /c/Users/User/students – Uniqueflave Aug 07 '21 at 11:39
  • Does stripping /Anything/until/home a viable answer? If so, some unix tools like `sed` could do this for you. I don't use git bash, do you have `sed` in it? – iuridiniz Aug 07 '21 at 11:53
  • try this: `for p in "/c/Users/User/students/home/users/jakubo" "/c/Users/User/students/home/users/anna" ; do echo "$p --> $(echo $p | cut -d/ -f 1,6-)" ;done` – iuridiniz Aug 07 '21 at 12:04
  • or try this: `for p in "/c/Users/User/students/home/users/jakubo" "/c/Users/User/students/home/users/anna" ; do echo "$p --> ${p:22}" ;done` – iuridiniz Aug 07 '21 at 12:05
  • 1
    Just use `find home` instead of `find "$(pwd)"/home`. – Socowi Aug 07 '21 at 12:19
  • if you don't need the first `/` just use `find home` instead of `find "$(pwd)"/home` like @Socowi said – iuridiniz Aug 07 '21 at 12:26
  • Does this answer your question? [How to get $HOME directory of different user in bash script?](https://stackoverflow.com/questions/20504662/how-to-get-home-directory-of-different-user-in-bash-script) – Léa Gris Aug 07 '21 at 12:48

1 Answers1

0

If removing unneeded strings is viable for you, just strip them:

Using bash substring (${var:lenght}) your shownames.sh will be:

for iter in "$@"; do find "$(pwd)"/home -type d -name "*$iter*"; done | while read p; do echo ${p:22}; done

results:

root@fd416de714ab:/c/Users/User/students# ./shownames.sh anna jakub
/home/users/anna
/home/users/jakubo
root@fd416de714ab:/c/Users/User/students#

Using cut tool, your shownames.sh will be:

for iter in "$@"; do find "$(pwd)"/home -type d -name "*$iter*"; done | cut -d/ -f 1,6-

results will be the same

Also, the $(pwd)/ seems unnecessary:

for iter in "$@"; do find ./home -type d -name "*$iter*"; done | while read p; do echo ${p:1}; done
iuridiniz
  • 2,213
  • 24
  • 26
  • You can not be sure the home directory of a user contains the user id in its path. – Léa Gris Aug 07 '21 at 12:49
  • This answer is very restrict to the question, I just explain how to remove unneeded parts @LéaGris – iuridiniz Aug 07 '21 at 12:52
  • Also, he uses gitbash (windows), if he was using linux, I could get the home directories from /etc/passwd, but it's not that case @LéaGris – iuridiniz Aug 07 '21 at 13:01
  • As an aside, if you omit the completely unnecessary `$(pwd)` you only have to remove `.` from the beginning of each result. – tripleee Aug 07 '21 at 14:06