Home of user that is stored in var

I have a user name that is stored in variable $i
and i want to use that user's home dirctor in case command

something like this

find ~"$i" |while read p
do
case "$p" in
( ~"$i"/myDir )
echo "$p"
;;
(*)
esac
done

but it doesn't work

some help please

Please define "doesn't work" like error messages or unexpected behavior.
Your script sample works fine with ksh.

In ksh, eval runs your line through the ksh one more time:

$( eval echo ~$i )

or

$( echo echo ~$i | ksh )

or in your case:

eval find ~$i . . . | . . . .

You can go get it yourself from /etc/passwd's final field.

1 Like

it show :
find: '~jan44' : no such file or director

where jan44 is the value of the $i
and i have a user with name jan44 on my system " debian" and i use bash

Eval isn't required with ksh but will fix this script when run by bash:

ihome=$(eval echo ~$i)
find $ihome |while read p
do
  case "$p" in
  ($ihome/myDir)
    echo "$p"
  ;;
  esac
done
1 Like

it works Thanks

Maybe I have an old ksh? Revision: 82.10.1.61

You can do the eval right on the find, saving much clutter.

Hmm, looks like more an IP address than a ksh version to me (ksh versions are mainly 88 and 93 with a letter appended to mean the release) but anyway you are right. I tested on ksh93 but ksh88 which you must be running has the same issue as bash.

Doing an eval for each file found in the home directory instead of once would hurt performance.