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
Share:
31,139

Related videos on Youtube

Marlon--
Author by

Marlon--

Updated on September 18, 2022

Comments

  • Marlon--
    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
      vonbrand about 11 years
      At least csh, bash use ~user for the user's home. It is the value of the shell variable HOME for each user.
    • Marlon--
      Marlon-- about 11 years
      I've tried to change the above script example to use ~user with the mateconftool-2 command but, its not working? I've edited my post to reflect this.
  • daisy
    daisy about 11 years
    @tijko getent returns colon separated lines, so $6 would be the sixth element, which is your home directory
  • rahmu
    rahmu about 11 years
    Honest 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
    daisy about 11 years
    @rahmu hmm, I'm not sure, but the OP runs mint, so that's okay here ...