脚本功能
- 命令行参数指定ip连接/获取ifconfig中的本地ip连接
- 修改.bashrc
#!/bin/bash
net_dev="wifi0" #默认的设备名
FALSE="0"
TRUE="1"
# 若无参数则选择本地ipv4 通过正则匹配ifconfig $net_dev的内容
l_ip=$(ifconfig $net_dev | grep "inet" | grep -v "inet6" |
sed 's/^.*inet//g' | sed 's/netmask.*$//g' | sed 's/ //g')
ok_ip=$l_ip
is_ok=$FALSE
m_ip=$1 #命令行指定参数获取
if [ "$m_ip" = "" ];then
is_ip=$(echo $l_ip | grep -P "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}")
if [ "$is_ip" = "" ];then
echo "ip is wrong or not found"
else
echo "use local ip"
is_ok=$TRUE
fi
else
is_ip=$(echo $m_ip | grep -P "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}")
if [ "$is_ip" = "" ];then
echo "inputed ip is wrong ,you can retry or using default ip"
else
echo "use inputed ip"
is_ok=$TRUE
ok_ip=$is_ip
fi
fi
if [ "$is_ok" = "1" ];then
echo "Waiting for xfce connected to $ok_ip open"
sed -i "$ s/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/$ok_ip/" ./.bashrc
source ./.bashrc
startxfce4
fi
注意事项
- 脚本修改的是.bashrc最后一行中的ip
export DISPLAY=192.168.x.x:0
- 因为脚本中有
source
命令,所以在终端中执行命令时要加source
# 本地ipv4执行
source ./openxfce4.sh
# 指定ip执行
source ./openxfce4.sh ip_addr
- 执行脚本前先启动
xfce4