bash 脚本中的用户主路径

tij*_*jko 4 bash cron shell-script environment-variables home

我正在编写一个 bash 脚本,它将每天作为 cron 作业运行。非常基本,我想每天更换壁纸。我和伙伴有 mint-14。

我现在遇到的事情是,我希望自动检测用户的主路径。如果我不这样做,我将不得不为运行该脚本的所有其他用户更改它。

到目前为止,我已经尝试过:

homedir=${HOME}/Pictures/daily

mateconftool-2 -t string -s /desktop/mate/background/picture_filename $homedir;
Run Code Online (Sandbox Code Playgroud)

这不起作用,但是,

echo $homedir
Run Code Online (Sandbox Code Playgroud)

打印出正确的路径?

编辑:

当我尝试~user像@vonbrand 暗示没有区别时。

mateconftool-2 -t string -s /desktop/mate/background/picture_filename ~user/Pictures/daily;
Run Code Online (Sandbox Code Playgroud)

dai*_*isy 5

$HOME 没有在 cron 中设置,所以把它放在一个脚本中,让你的 cron 作业执行它,

(记得用 chmod +x XX 设置该脚本的执行位)

#!/bin/bash

mateconftool-2 -t string -s /desktop/mate/background/picture_filename ~/Pictures/daily
Run Code Online (Sandbox Code Playgroud)

或者在你的定时任务中,

HOME="$(getent passwd $USER | awk -F ':' '{print $6}')"
homedir=${HOME}/Pictures/daily
Run Code Online (Sandbox Code Playgroud)