树莓派(raspberrypi os)shell脚本及脚本开机自启

1、shell脚本运行.py文件

\#!/bin/bash

\#到/xx/xx目录下并运行xx.py文件

cd /home/xx/xx && python xx.py

2、shell脚本其他简单操作

\#!/bin/bash

#循环操作 输出字符串操作

while true;do

​	echo "new day set ok"

done

#打印日期时间

#年月日

current_date=$(date +"%Y%m%d")

#时分秒

current_hour=$(date +"%H%M%S")

#年月日时分秒

current_time=$(date +"%Y%m%d%H%M%S")

#输出变量操作

echo $current_date

echo $current_hour

echo $current_time

#判断选择结构

flag1=0

flag2=1

#-eq 等于

if [ $flag1 -eq 0 ];then

​	echo "flag1=0"

fi

#-ne 不等于

if[ $flag2 -ne 0 ];then

​	echo "flag2!=0"

fi

if[ $flag1 -ne $flag2 ];then

​	echo $flag1 

​	echo $flag2 

​	echo "flag1!=flag2 "

fi

#自加自减

flag1=0

flag2=100

((flag1++))

((flag2--))

#创建目录 创建目录/xx/yy/zz

 mkdir -p /home/name/xx/yy/zz

#创建文件 到目录/home/name/xx/yy/zz下创建 test.txt文件

cd /home/name/xx/yy/zz

touch test.txt

#复制目录 复制目录/xx下所有内容到/yy下

cp -r /home/name/xx/* /home/name/yy

#删除目录 删除目录/xx下所有内容

rm /home/name/xx/*

3、开机自启脚本

参考https://blog.csdn.net/zhinian_hao/article/details/129104387?sharetype=blog&shareId=129104387&sharerefer=APP&sharesource=weixin_46402470&sharefrom=linkicon-default.png?t=O83Ahttp://树莓派设置开机自启动的三种方式 - CSDN App

1、ctrl+h显示隐藏文件夹、或终端进入/home/name/.config 路径

2、查看.config 目录中是否有autostart文件夹(没有就创建一个mkdir autostart)

3、进入autostart 中创建start.desktop文件(touch start.desktop名称任意,结尾为.desktop)

4、打开文件start.desktop(vim start.desktop或gedit start.desktop)写入内容

[Desktop Entry]

Name=start

Icon=utilities-terminal

Terminal=false

Type=Application

Exel=sh /home/name/xx/xx.sh

从上至下内容依次为

#名称

#图标为终端图标(也可自己设置)

#不启动终端

#类型

#启动/home/name/目录下xx.sh脚本

5、完成后保存退出关闭隐藏文件夹

6、使用shell脚本启动.py文件,或做简单逻辑,使用.desktop自启shell脚本

7、重启测试完成(sudo reboot)

猜你喜欢

转载自blog.csdn.net/weixin_46402470/article/details/143240889
今日推荐