shell sed修改linux终端的配置文件

Posted EbowTang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell sed修改linux终端的配置文件相关的知识,希望对你有一定的参考价值。

这是一个简单,但是完整的案例,实现

1,对修改配置的恢复./set_spaceos.sh  -r

2,执行后,在main.conf配置文件内容里,添加指定内容

#! /bin/bash
##############################################################################
# Date Created: 2021-08-03
#       Author: EbowTang
#  Description: set x64 spaceos
#       object: H3C Workspace E1009h10
#    Return  0: Success
#-----------------------------------------------------------------------------
#  Modification History:
#  DATE        NAME             DESCRIPTION
#  2021-08-03  set_spaceos      set spaceos audio(disable audio by hdmi) and set phone redirect in workspace client
#  Examples of usage:
#  SCRIPT            PARAMETER  DESCRIPTION
#  ./set_spaceos.sh             #set spaceos audio(disable audio by hdmi) set phone redirect in workspace client
#  ./set_spaceos.sh  -r         #remove all set and restore to initial state
#-----------------------------------------------------------------------------
#  Other tips:
#  1, If the script has been executed once, all settings will not take effect after the second execution
##############################################################################
LOG_FILE=/var/log/set_spaceos.log
exec >>$LOG_FILE 2>&1
echo $(date) >$LOG_FILE

if [ `whoami` != 'root' ]; then
	echo -e "User checking failure: You need root privileges to run this script."
	exit 1
fi

CLIENT_VERSION=$(cat /userdata/H3C/Workspace/CURRENT)
SPACEOS_VERSION=$(cat /etc/spaceos-version | awk 'print $2')

key=$1
if [ "$key" == "-r" ]; then
	#restore to initial state
	cd "/userdata/H3C/Workspace/$CLIENT_VERSION/Device Redirect/"
	cp main.conf.back main.conf
	cd /etc/pulse/
	cp default.pa.back default.pa
	rm -rf /root/doneset.flag
	echo "remove all successful"
	#sed -i 's/#set-default-sink alsa_output.pci-0000_00_01.1.hdmi-stereo/set-default-sink alsa_output.pci-0000_00_01.1.hdmi-stereo/g' /etc/pulse/default.pa
	#sed -i 'set-card-profile alsa_card.pci-0000_00_14.2 output:analog-stereo+input:analog-stereo/d' /etc/pulse/default.pa
	#sed -i 'set-default-sink alsa_output.pci-0000_00_14.2.analog-stereo/d' /etc/pulse/default.pa
	#sed -i 'set-default-source alsa_input.pci-0000_00_14.2.analog-stereo/d' /etc/pulse/default.pa
	exit 1
fi

if [ -f /root/doneset.flag ]; then
	echo work had done!
	exit 1
fi

#1,set spaceos audio(disable audio by hdmi)
echo “pacmd dump before”
pacmd dump
if [ ! -f /etc/pulse/default.pa ]; then
	echo there is no default.pa,please check!
	exit 1
fi
if [ ! -f /etc/pulse/default.pa.back ]; then
	cp /etc/pulse/default.pa /etc/pulse/default.pa.back
fi
cd /etc/pulse/
echo "start to set........."
sed -i 's/set-default-sink alsa_output.pci-0000_00_01.1.hdmi-stereo/#set-default-sink alsa_output.pci-0000_00_01.1.hdmi-stereo/g' /etc/pulse/default.pa
echo "set-card-profile alsa_card.pci-0000_00_14.2 output:analog-stereo+input:analog-stereo" >> /etc/pulse/default.pa
echo "set-default-sink alsa_output.pci-0000_00_14.2.analog-stereo" >> /etc/pulse/default.pa
echo "set-default-source alsa_input.pci-0000_00_14.2.analog-stereo" >> /etc/pulse/default.pa
sleep 1
echo “pacmd dump after”
pacmd dump


#2,set phone redirect in workspace client
cd "/userdata/H3C/Workspace/$CLIENT_VERSION/Device Redirect/"
if [ ! -f "/userdata/H3C/Workspace/$CLIENT_VERSION/Device Redirect/main.conf" ]; then
	echo there is no main.conf,please check!
	exit 1
fi
if [ ! -f main.conf.back ]; then
	cp main.conf main.conf.back
fi
#xiaomi 图片传输USB\\VID_2717&PID_FF10\\C1CF12C4,存储模式USB\\VID_0A9D&PID_FF40\\C1CF12C4
#oppo   照片模式USB\\VID_22D9&PID_2771\\776EA019,oppo   存储模式USB\\VID_c\\776EA019
#rongyao 图片传输USB\\VID_12D1&PID_107E&MI_00\\6&7C86353&0&0000,rongyao 存储模式USB\\VID_12D1&PID_107E&MI_02\\6&7C86353&0&0002
#one plus 存储模式USB\\VID_22D9&PID_2771\\DD9329EB,one plus 图片传输USB\\VID_22D9&PID_2771\\DD9329EB
#huawei 图片传输/存储模式,USB\\VID_12D1&PID_107E&MI_00\\6&3B387317&0&0000
#iphone 默认支持,无需配置
srckey=$(cat main.conf)
#注意全部需要改为小写
#如果需要增加新的手机设备,可以在keyset=$srckey"2717:ff10;0a9d:ff40;22d9:2771;22d9:2764;12d1:107e;"末尾加上该设备"VID:PID"(不包含引号,全部小写);
#比如某品牌手机VID是0000,PID是1010,则直接在下列命令尾部后引号加上"0000:1010;",即
#添加前:keyset=$srckey"2717:ff10;0a9d:ff40;22d9:2771;22d9:2764;12d1:107e;"
#添加后:keyset=$srckey"2717:ff10;0a9d:ff40;22d9:2771;22d9:2764;12d1:107e;0000:1010;"
keyset=$srckey"2717:ff10;0a9d:ff40;22d9:2771;22d9:2764;12d1:107e;339b:107d;2717:ff48;"
sed -i "s#$srckey#$keyset#g" main.conf
sleep 1
touch /root/doneset.flag
reboot

以上是关于shell sed修改linux终端的配置文件的主要内容,如果未能解决你的问题,请参考以下文章

如何通过 `awk` 或 `sed` 在 Ubuntu 终端变量的末尾添加一些字符?

Linux之Shell编程(15)--sed关键字详解演示

Linux之Shell编程(15)--sed关键字详解演示

Linux Shell常用技巧 sed

Shell脚本 使用sed流编辑器一键修改CentOS网卡IP地址

linux shell 之在线文本编辑sed