Users home path in a bash script
31,139
$HOME is not set in cron, so put this in a script, and let your cron job execute that instead,
(Remember to set the execution bit for that script with chmod +x XX)
#!/bin/bash
mateconftool-2 -t string -s /desktop/mate/background/picture_filename ~/Pictures/daily
Or in your cronjob,
HOME="$(getent passwd $USER | awk -F ':' '{print $6}')"
homedir=${HOME}/Pictures/daily
Related videos on Youtube
Author by
Marlon--
Updated on September 18, 2022Comments
-
Marlon-- over 1 year
I'm writing a bash-script that will be run as a cron job everyday. Very basic, I was wanting to change the wallpaper daily. I have mint-14 with mate.
The thing I'm getting caught up on right now is, I want to have the user's home path detected automatically. If I don't do this I would have to change it for all other users that run the script.
So far I have tried:
homedir=${HOME}/Pictures/daily mateconftool-2 -t string -s /desktop/mate/background/picture_filename $homedir;
This doesn't work but,
echo $homedir
Prints out the correct path?
EDIT:
When I tried
~user
like @vonbrand was suggesting there is no difference.mateconftool-2 -t string -s /desktop/mate/background/picture_filename ~user/Pictures/daily;
-
vonbrand about 11 yearsAt least csh, bash use
~user
for the user's home. It is the value of the shell variableHOME
for each user. -
Marlon-- about 11 yearsI've tried to change the above script example to use
~user
with themateconftool-2
command but, its not working? I've edited my post to reflect this.
-
-
daisy about 11 years@tijko getent returns colon separated lines, so $6 would be the sixth element, which is your home directory
-
rahmu about 11 yearsHonest question: how portable is it? Is it safe to assume that the user's home directory is the 6th element of the
/etc/password
file for several (most) unices? -
daisy about 11 years@rahmu hmm, I'm not sure, but the OP runs mint, so that's okay here ...