xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

How to execute a shell script in the .profile file All In One

How to execute a shell script in the .profile file All In One

image

image

.profile 用户级启动配置文件

image

https://www.cnblogs.com/xgqfrms/p/17343088.html

demos

When use the SSH to login the Raspberry Pi, it will be auto send it's IP address to a chat group's notice message robot 🤖

https://github.com/xgqfrms/dd-robot-notice

$ cat ./dd-ip-notice-robot.sh

#!/usr/bin/env bash
# coding: utf8

# 自动发送树莓派 ip 地址,到钉钉上
DD_ROBOT_TOKEN=404e996c8747ea4a1230f5cd5f7b2d36006f2732f9111bd3f39ce36d17fa1202
echo $DD_ROBOT_TOKEN

WIFI=$(ifconfig | grep "192.168")

# ✅ $(可执行命令)
# $(ifconfig | grep "192.168" > ip.md)
$(ifconfig | perl -nle'/(\d+\.\d+\.\d+\.\d+)/ && print $1' | sed '1 d' > ip.md)

# ✅ read file
RPI_IP=$(cat ./ip.md)

Smbol=$(echo '$')

echo $RPI_IP

MARKDOWN='
{
  "msgtype": "markdown",
  "markdown": {
    "title":"🤖 树莓派开机自动发送 Wi-Fi IP 地址消息: ",
    "text": "# 🍓 Raspberry Pi IP: ['${RPI_IP}']('$RPI_IP') \n
> 🔑 API Token: '$WIFI'

# ⚠️ ⚠️ ⚠️ 这里临时做的字符转移处理是为了,修复 markdown code nested bug
\`\`\`sh
#!/usr/bin/env bash
# coding: utf8

'$Smbol' hostname -d

\`\`\`
# ⚠️ ⚠️ ⚠️

  多行文本换行测试 ✅ 
缩进测试 ❌ 不好使 
\n 不好使,markdown 末尾空格换行 🚀
    "
  },
  "at": {
    "atUserIds": [
      "xgqfrms"
    ],
    "isAtAll": false
  }
}'
# echo $MARKDOWN

curl "https://oapi.dingtalk.com/robot/send?access_token=$DD_ROBOT_TOKEN" \
  -H "Content-Type: application/json" \
  -d "$MARKDOWN"

echo "finished ✅"


exit 0
$ cat ./ip-program.sh
#!/usr/bin/env bash
# coding: utf8

# 自定义登录启动脚本
/usr/bin/bash -u /home/pi/Desktop/dd-ip-notice-robot.sh > /home/pi/Desktop/ip-program.log 2>&1
# 1. modify
$ sudo vim ~/.profile

# 2. update
$ source ~/.profile

# 3. check
$ cat ~/.profile

# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
	. "$HOME/.bashrc"
    fi
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
    PATH="$HOME/.local/bin:$PATH"
fi

echo "每次登录自动执行脚本 ✅"
bash /home/pi/Desktop/ip-program.sh

test OK ✅

$ ssh pi@raspberrypi.local
pi@raspberrypi.local's password: 
Linux raspberrypi 5.15.84-v8+ #1613 SMP PREEMPT Thu Jan 5 12:03:08 GMT 2023 aarch64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
You have no mail.
Last login: Mon Apr 17 11:18:29 2023 from 192.168.18.195

SSH is enabled and the default password for the 'pi' user has not been changed.
This is a security risk - please login as the 'pi' user and type 'passwd' to set a new password.

每次登录自动执行脚本 ✅

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

raspi-config

# GUI 交互式命令行界面
$ sudo raspi-config

在本质上还是直接修改 /boot/config.txt 配置文件

https://www.raspberrypi.com/documentation/computers/configuration.html#the-raspi-config-tool

refs

https://unix.stackexchange.com/questions/39644/how-can-i-profile-a-shell-script

https://superuser.com/questions/299394/how-to-automatically-execute-a-shell-script-when-logging-into-ubuntu

https://stackoverflow.com/questions/22913551/how-to-run-profile-inside-a-shell-script-in-ubuntu/22913615#22913615



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2023-04-17 16:48  xgqfrms  阅读(17)  评论(4编辑  收藏  举报