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

发布时间 2023-04-17 16:54:23作者: xgqfrms

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

image

image

demos

$ 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/ 查看原创文章!

refs

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, 禁止转载 ?️,侵权必究⚠️!