CSDN仅用于增加百度收录权重,排版未优化,日常不维护。请访问:www.hceng.cn 查看、评论。
本博文对应地址: https://hceng.cn/2017/08/05/Linux开发环境配置及shell-script/#more
本文主要是以快速搭建环境为目标学习shell script。
之前写过一个Linux嵌入式开发环境搭建的博客,后面每次搭环境都翻来复制上面的代码。感觉就像记事本一样,还是有点用,这也是写博客的一点动力吧。
用了Linux也有段时间了,它的魅力也逐渐展现出来了。以前还在熟悉Linux命令的过程中,shell script就是个老虎,觉得没接触过,害怕,每次都绕开它。
直到这周遇到了ti的SDK,尝试一边百度,一边测它的脚本,感觉有点小入门了,于是想练习下。想到每次搭建环境重复操作的痛点,就拿它开刀吧。
0.环境搭建的几个方面
根据过去的经验,用虚拟机装好Ubuntu后,一般会做这样几个步骤:
1.安装vmware tools,以便复制粘贴和文件共享;
2.更新软件源,以便更快的下载软件;
3.安装及配置常用软件,比如:
3-1.安装git,方面后续的一些安装;
3-2.安装vim和简单配置;
3-3.安装ftp和简单配置;
3-4.安装nfs和简单配置;
3-5.安装samba和简单配置;
3-6.安装tmux、htop等;
4.安装开发所需的 g++等工具、库;
除了第一步,其它都计划用脚本实现。
在开发中,厌烦了每次编辑了驱动,都要拖进Linux主机进行编译,然后还要复制到开发板。
重新打造了工作流,只需要在电脑上操作三步:
编辑->make编译->insmod加载
1.检测当前环境
在运行脚本前,需要检测一些东西:
1.1检测网络状态
如果不能联网,后续的没必要做了。之前想过自动检测修复网络的,但想了想,网络的情况比较多,暂时搁置。而且刚装好虚拟机肯定是联网的,如果不能联网肯定是虚拟机设置的问题,也不是脚本能解决的。
为了方便打印醒目和更改用户名,这里先设置几个变量:
{% codeblock lang:bash%}
#define echo print color.
RED_COLOR=’\E[1;31m’
PINK_COLOR=’\E[1;35m’
YELOW_COLOR=’\E[1;33m’
BLUE_COLOR=’\E[1;34m’
GREEN_COLOR=’\E[1;32m’
END_COLOR=’\E[0m’
#set linux host user name.
user_name=hceng
{% endcodeblock %}
检测网络函数:
{% codeblock lang:bash%}
check_network() {
ping -c 1 www.baidu.com > /dev/null 2>&1
if [
?
−
e
q
0
]
;
t
h
e
n
e
c
h
o
−
e
"
? -eq 0 ];then echo -e "
?−eq0];thenecho−e"{GREEN_COLOR}Network ok.
E
N
D
C
O
L
O
R
"
e
l
s
e
e
c
h
o
−
e
"
{END_COLOR}" else echo -e "
ENDCOLOR"elseecho−e"{RED_COLOR}Network failure!${END_COLOR}"
exit 1
fi
}
{% endcodeblock %}
先ping一次百度,能够ping通就表示网络没问题。
1.2检测是否是root用户
很多操作都需要root权限,因此必须使用root用户权限运行脚本,这里先检测是否是root用户:
{% codeblock lang:bash%}
Check user must root.
check_root() {
if [
(
i
d
−
u
)
!
=
"
0
"
]
;
t
h
e
n
e
c
h
o
−
e
"
(id -u) != "0" ]; then echo -e "
(id−u)!="0"];thenecho−e"{RED_COLOR}Error: You must be root to run this script, please use root.${END_COLOR}"
exit 1
fi
}
{% endcodeblock %}
1.3检测设置的用户是否存在
现在用的用户名是hceng,后面为了方便其它用户使用,所以在前面定义了个变量,改变变量就可以修改用户。但为了防止设置的用户在系统中不存在,这里需要进行检测。
{% codeblock lang:bash%}
Check set linux host user name.
check_user_name() {
cat /etc/passwd|grep $user_name
if [
?
−
e
q
0
]
;
t
h
e
n
e
c
h
o
−
e
"
? -eq 0 ];then echo -e "
?−eq0];thenecho−e"{GREEN_COLOR}Check the set user name OK.
E
N
D
C
O
L
O
R
"
e
l
s
e
e
c
h
o
−
e
"
{END_COLOR}" else echo -e "
ENDCOLOR"elseecho−e"{RED_COLOR}Check the set user name failure!${END_COLOR}"
exit 1
fi
}
{% endcodeblock %}
1.4检测运行结果
为检测某些命令,是否运行正常。
{% codeblock lang:bash%}
check_status() {
ret=
?
i
f
[
"
? if [ "
?if["ret" -ne “0” ]; then
echo -e “
R
E
D
C
O
L
O
R
F
a
i
l
e
d
s
e
t
u
p
,
a
b
o
r
t
i
n
g
.
.
{RED_COLOR}Failed setup, aborting..
REDCOLORFailedsetup,aborting..{END_COLOR}”
exit 1
fi
}
{% endcodeblock %}
1.5获取系统版本信息
后面更新源和装某些软件需要当前Ubuntu的版本代号。
{% codeblock lang:bash%}
Get the code name of the Linux host release to the caller.
get_host_type() {
local __host_type=$1
local the_host=lsb_release -a 2>/dev/null | grep Codename: | awk {'print $2'}
eval KaTeX parse error: Expected group after '_' at position 1: _̲_host_type="'the_host’"
}
{% endcodeblock %}
2.更新软件源
这里使用了阿里、网易和官方的三个源,应该没问题了。
修改配置文件的原则就是先备份再修改。同时为了防止脚本再次运行覆盖掉备份,还需要检测是否已经存在了备份。
{% codeblock lang:bash%}
#This function will update the source of the software.
update_software_source() {
local back_file=/etc/apt/sources.list.backup
if [ ! -e “$back_file” ];then
cp /etc/apt/sources.list $back_file
fi
check_status
get_host_type host_release
check_status
echo \
"#Ali source.
deb-src http://archive.ubuntu.com/ubuntu $host_release main restricted
deb http://mirrors.aliyun.com/ubuntu/ $host_release main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ $host_release main restricted multiverse universe
deb http://mirrors.aliyun.com/ubuntu/ $host_release-updates main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ $host_release-updates main restricted multiverse universe
deb http://mirrors.aliyun.com/ubuntu/ $host_release universe
deb http://mirrors.aliyun.com/ubuntu/ $host_release-updates universe
deb http://mirrors.aliyun.com/ubuntu/ $host_release multiverse
deb http://mirrors.aliyun.com/ubuntu/ $host_release-updates multiverse
deb http://mirrors.aliyun.com/ubuntu/ $host_release-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ $host_release-backports main restricted universe multiverse
deb http://archive.canonical.com/ubuntu $host_release partner
deb-src http://archive.canonical.com/ubuntu $host_release partner
deb http://mirrors.aliyun.com/ubuntu/ $host_release-security main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ $host_release-security main restricted multiverse universe
deb http://mirrors.aliyun.com/ubuntu/ $host_release-security universe
deb http://mirrors.aliyun.com/ubuntu/ $host_release-security multiverse
#Netease source.
deb http://mirrors.163.com/ubuntu/ $host_release main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ $host_release-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ $host_release-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ $host_release-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ $host_release-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ $host_release main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ $host_release-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ $host_release-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ $host_release-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ $host_release-backports main restricted universe multiverse
#Official source
deb http://archive.ubuntu.com/ubuntu/ $host_release main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ $host_release-security main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ $host_release-updates main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ $host_release-proposed main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ $host_release-backports main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ $host_release main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ $host_release-security main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ $host_release-updates main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ $host_release-proposed main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ $host_release-backports main restricted universe multiverse" \
> /etc/apt/sources.list
check_status
#apt-get update 1>/dev/null
apt-get update
check_status
echo -e "${GREEN_COLOR}Update source completed.${END_COLOR}"
}
{% endcodeblock %}
3.安装软件及配置
软件主要包括自己一般常用的软件,像vim、tmux、samba等。还有就是开发所需的g++、各种库。
其中git要在vim前面,因为vim的配置文件需要git下载。
{% codeblock lang:bash%}
Execute an action.
FA_DoExec() {
echo -e “
B
L
U
E
C
O
L
O
R
=
=
>
E
x
e
c
u
t
i
n
g
:
′
{BLUE_COLOR}==> Executing: '
BLUECOLOR==>Executing:′{@}’.${END_COLOR}”
eval $@ || exit $?
}
Install list software.
install_software() {
local install_software_list=(“git” “vim” “tmux” “htop” “vsftpd” “openssh-server” “nfs-kernel-server” “portmap” “samba”)
echo -e “
P
I
N
K
C
O
L
O
R
i
n
s
t
a
l
l
s
o
f
t
w
a
r
e
l
i
s
t
:
{PINK_COLOR}install_software_list:
PINKCOLORinstallsoftwarelist:{install_software_list[*]}.${END_COLOR}”
#install git
if (echo "${install_software_list[@]}" | grep -wq "git");then
apt-get -y install git && echo -e "${BLUE_COLOR}git install completed.${END_COLOR}"
fi
#install and configure vim
if (echo "${install_software_list[@]}" | grep -wq "vim");then
apt-get -y install vim && vim_configure && echo -e "${BLUE_COLOR}vim install completed.${END_COLOR}"
fi
#install tmux
if (echo "${install_software_list[@]}" | grep -wq "tmux");then
apt-get -y install tmux && echo -e "${BLUE_COLOR}tmux install completed.${END_COLOR}"
fi
#install htop
if (echo "${install_software_list[@]}" | grep -wq "htop");then
apt-get -y install htop && echo -e "${BLUE_COLOR}htop install completed.${END_COLOR}"
fi
#install and configure vsftpd
if (echo "${install_software_list[@]}" | grep -wq "vsftpd");then
apt-get -y install vsftpd && ftp_configure && echo -e "${BLUE_COLOR}vsftpd install completed.${END_COLOR}"
fi
#install openssh-server
if (echo "${install_software_list[@]}" | grep -wq "openssh-server");then
apt-get -y install openssh-server && echo -e "${BLUE_COLOR}openssh-server install completed.${END_COLOR}"
fi
#install and configure nfs-kernel-server
if (echo "${install_software_list[@]}" | grep -wq "nfs-kernel-server");then
apt-get -y install nfs-kernel-server && nfs_configure && \
/etc/init.d/nfs-kernel-server restart && echo -e "${BLUE_COLOR}nfs-kernel-server install completed.${END_COLOR}"
fi
#install portmap
if (echo "${install_software_list[@]}" | grep -wq "portmap");then
apt-get -y install portmap && echo -e "${BLUE_COLOR}portmap install completed.${END_COLOR}"
fi
#install and configure samba
if (echo "${install_software_list[@]}" | grep -wq "samba");then
apt-get -y install samba && samba_configure && echo -e "${BLUE_COLOR}samba install completed.${END_COLOR}"
fi
#others
get_host_type host_release
FA_DoExec apt-get -y install \
gnupg flex bison gperf build-essential \
zip curl libc6-dev libncurses5-dev libncurses5-dev:i386 x11proto-core-dev \
libx11-dev:i386 libreadline6-dev:i386 \
libgl1-mesa-glx-lts-$host_release:i386 libgl1-mesa-dev-lts-$host_release \
g++-multilib mingw32 tofrodos libncurses5-dev:i386 \
python-markdown libxml2-utils xsltproc zlib1g-dev:i386
if [ ! -h /usr/lib/i386-linux-gnu/libGL.so ]; then
FA_DoExec ln -s /usr/lib/i386-linux-gnu/mesa/libGL.so.1 \
/usr/lib/i386-linux-gnu/libGL.so
fi
# Development support
FA_DoExec apt-get -y install \
dos2unix minicom gawk
echo -e "${GREEN_COLOR}software install completed.${END_COLOR}"
}
{% endcodeblock %}
3.1配置vim
vim的配置文件来自GitHub的Amir,我就不重复造轮子了。
这里只是先clone下来,然后执行安装脚本。这里值得一提的是执行脚本要指定用户运行,不然会以root用户的路径设置,导致脚本运行错误。
{% codeblock lang:bash%}
Configure vim form github.
vim_configure() {
git clone --depth=1 https://github.com/amix/vimrc.git /home/$user_name/.vim_runtime
touch /home/$user_name/.vim_runtime/my_configs.vim
echo ":set number" > /home/$user_name/.vim_runtime/my_configs.vim
chown -R $user_name /home/$user_name/.vim_runtime
chmod u+x /home/$user_name/.vim_runtime/install_awesome_vimrc.sh
su - $user_name -s /home/$user_name/.vim_runtime/install_awesome_vimrc.sh
}
{% endcodeblock %}
3.2配置ftp
FTP主要是修改为可写。在使用MobaXterm SSH登陆后,可以直接通过左边的Sftp进行文件的传输,貌似这个可以不用配置了。
{% codeblock lang:bash%}
Configure ftp.
ftp_configure() {
sed -i ‘s/#loacl_enable=YES/loacl_enable=YES/g’ /etc/vsftpd.conf
sed -i ‘s/#write_enable=YES/write_enable=YES/g’ /etc/vsftpd.conf
}
{% endcodeblock %}
3.3配置nfs
在前面的工作流中,开发板直接运行Linux主机中交叉编译好的模块,是通过nfs实现的。因此需要开发板开机后就挂载nfs.
习惯在Linux主机中单独开辟一个路径作为工作目录,因此这里是设置的整个工作目录。
{% codeblock lang:bash%}
Configure nfs.
nfs_configure() {
local work_file=/work
if [ ! -d “$work_file” ];then
mkdir /work
fi
check_status
grep "/work" /etc/exports 1>/dev/null
if [ $? -ne 0 ];then
sed -i '$a\/work *(rw,sync,no_root_squash,no_subtree_check)' /etc/exports
fi
}
{% endcodeblock %}
3.4配置samba
samba也是实现前面工作流不可确少的一环。这样就不用每次修改代码后,通过ftp上传到Linux主机。
samba在Windows上的使用是:
先Windows+r
,打开命令窗口,
然后输入:\\192.168.1.xx
,
最后建议右键,映射网络驱动,方面后续打开。
测试中需要重启生效。
{% codeblock lang:bash%}
Configure samba.
samba_configure() {
local back_file=/etc/samba/smb.conf.bakup
if [ ! -e “$back_file” ];then
cp /etc/samba/smb.conf $back_file
fi
check_status
grep "/work" /etc/samba/smb.conf 1>/dev/null
if [ $? -ne 0 ];then
sed -i \
'$a[share_work]\n\
path = \/work\n\
available = yes\n\
public = yes\n\
guest ok = yes\n\
read only = no\n\
writeable = yes\n' /etc/samba/smb.conf
fi
/etc/init.d/samba restart
chmod -R 777 /work
}
{% endcodeblock %}
4.完整代码及心得
{% codeblock lang:bash [setup_ubuntu_host_env.sh] https://github.com/hceng/learn/blob/master/shell/setup_ubuntu_host_env.sh%}
#!/bin/bash
-------------------------------------------------------------------------------
Filename: setup_ubuntu_host_env.sh
Revision: 1.0
Date: 2017/08/05
Author: hceng
Email: huangcheng.job@foxmail.com
Website: www.hceng.cn
Function: setup ubuntu host env.
Notes: learn
-------------------------------------------------------------------------------
Description:
#1.check env.
#1.1 check network
#1.2 check use root
#1.3 check set name
#1.4 configure samba
#2.update software sourcev.
#3.install vim tmux htop ftp ssh nfs samba.
#3.1 configure vim
#3.2 configure ftp
#3.3 configure nfs
#3.4 configure samba
#4.install system tool eg:g++ …
-------------------------------------------------------------------------------
#define echo print color.
RED_COLOR=’\E[1;31m’
PINK_COLOR=’\E[1;35m’
YELOW_COLOR=’\E[1;33m’
BLUE_COLOR=’\E[1;34m’
GREEN_COLOR=’\E[1;32m’
END_COLOR=’\E[0m’
#Set linux host user name.
user_name=hceng
Check network.
check_network() {
ping -c 1 www.baidu.com > /dev/null 2>&1
if [
?
−
e
q
0
]
;
t
h
e
n
e
c
h
o
−
e
"
? -eq 0 ];then echo -e "
?−eq0];thenecho−e"{GREEN_COLOR}Network OK.
E
N
D
C
O
L
O
R
"
e
l
s
e
e
c
h
o
−
e
"
{END_COLOR}" else echo -e "
ENDCOLOR"elseecho−e"{RED_COLOR}Network failure!${END_COLOR}"
exit 1
fi
}
Check user must root.
check_root() {
if [
(
i
d
−
u
)
!
=
"
0
"
]
;
t
h
e
n
e
c
h
o
−
e
"
(id -u) != "0" ]; then echo -e "
(id−u)!="0"];thenecho−e"{RED_COLOR}Error: You must be root to run this script, please use root.${END_COLOR}"
exit 1
fi
}
Check set linux host user name.
check_user_name() {
cat /etc/passwd|grep $user_name
if [
?
−
e
q
0
]
;
t
h
e
n
e
c
h
o
−
e
"
? -eq 0 ];then echo -e "
?−eq0];thenecho−e"{GREEN_COLOR}Check the set user name OK.
E
N
D
C
O
L
O
R
"
e
l
s
e
e
c
h
o
−
e
"
{END_COLOR}" else echo -e "
ENDCOLOR"elseecho−e"{RED_COLOR}Check the set user name failure!${END_COLOR}"
exit 1
fi
}
Check the results of the operation.
check_status() {
ret=
?
i
f
[
"
? if [ "
?if["ret" -ne “0” ]; then
echo -e “
R
E
D
C
O
L
O
R
F
a
i
l
e
d
s
e
t
u
p
,
a
b
o
r
t
i
n
g
.
.
{RED_COLOR}Failed setup, aborting..
REDCOLORFailedsetup,aborting..{END_COLOR}”
exit 1
fi
}
Get the code name of the Linux host release to the caller.
get_host_type() {
local __host_type=$1
local the_host=lsb_release -a 2>/dev/null | grep Codename: | awk {'print $2'}
eval KaTeX parse error: Expected group after '_' at position 1: _̲_host_type="'the_host’"
}
#This function will update the source of the software.
update_software_source() {
local back_file=/etc/apt/sources.list.backup
if [ ! -e “$back_file” ];then
cp /etc/apt/sources.list $back_file
fi
check_status
get_host_type host_release
check_status
echo \
"#Ali source.
deb-src http://archive.ubuntu.com/ubuntu $host_release main restricted
deb http://mirrors.aliyun.com/ubuntu/ $host_release main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ $host_release main restricted multiverse universe
deb http://mirrors.aliyun.com/ubuntu/ $host_release-updates main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ $host_release-updates main restricted multiverse universe
deb http://mirrors.aliyun.com/ubuntu/ $host_release universe
deb http://mirrors.aliyun.com/ubuntu/ $host_release-updates universe
deb http://mirrors.aliyun.com/ubuntu/ $host_release multiverse
deb http://mirrors.aliyun.com/ubuntu/ $host_release-updates multiverse
deb http://mirrors.aliyun.com/ubuntu/ $host_release-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ $host_release-backports main restricted universe multiverse
deb http://archive.canonical.com/ubuntu $host_release partner
deb-src http://archive.canonical.com/ubuntu $host_release partner
deb http://mirrors.aliyun.com/ubuntu/ $host_release-security main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ $host_release-security main restricted multiverse universe
deb http://mirrors.aliyun.com/ubuntu/ $host_release-security universe
deb http://mirrors.aliyun.com/ubuntu/ $host_release-security multiverse
#Netease source.
deb http://mirrors.163.com/ubuntu/ $host_release main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ $host_release-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ $host_release-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ $host_release-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ $host_release-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ $host_release main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ $host_release-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ $host_release-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ $host_release-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ $host_release-backports main restricted universe multiverse
#Official source
deb http://archive.ubuntu.com/ubuntu/ $host_release main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ $host_release-security main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ $host_release-updates main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ $host_release-proposed main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ $host_release-backports main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ $host_release main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ $host_release-security main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ $host_release-updates main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ $host_release-proposed main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ $host_release-backports main restricted universe multiverse" \
> /etc/apt/sources.list
check_status
#apt-get update 1>/dev/null
apt-get update
check_status
echo -e "${GREEN_COLOR}Update source completed.${END_COLOR}"
}
Configure vim form github.
vim_configure() {
git clone --depth=1 https://github.com/amix/vimrc.git /home/$user_name/.vim_runtime
touch /home/$user_name/.vim_runtime/my_configs.vim
echo ":set number" > /home/$user_name/.vim_runtime/my_configs.vim
chown -R $user_name /home/$user_name/.vim_runtime
chmod u+x /home/$user_name/.vim_runtime/install_awesome_vimrc.sh
su - $user_name -s /home/$user_name/.vim_runtime/install_awesome_vimrc.sh
}
Configure ftp.
ftp_configure() {
sed -i ‘s/#loacl_enable=YES/loacl_enable=YES/g’ /etc/vsftpd.conf
sed -i ‘s/#write_enable=YES/write_enable=YES/g’ /etc/vsftpd.conf
}
Configure nfs.
nfs_configure() {
local work_file=/work
if [ ! -d “$work_file” ];then
mkdir /work
fi
grep "/work" /etc/exports 1>/dev/null
if [ $? -ne 0 ];then
sed -i '$a\/work *(rw,sync,no_root_squash,no_subtree_check)' /etc/exports
fi
}
Configure samba.
samba_configure() {
local back_file=/etc/samba/smb.conf.bakup
if [ ! -e “$back_file” ];then
cp /etc/samba/smb.conf $back_file
fi
check_status
grep "/work" /etc/samba/smb.conf 1>/dev/null
if [ $? -ne 0 ];then
sed -i \
'$a[share_work]\n\
path = \/work\n\
available = yes\n\
public = yes\n\
guest ok = yes\n\
read only = no\n\
writeable = yes\n' /etc/samba/smb.conf
fi
/etc/init.d/samba restart
chmod -R 777 /work
}
Execute an action.
FA_DoExec() {
echo -e “
B
L
U
E
C
O
L
O
R
=
=
>
E
x
e
c
u
t
i
n
g
:
′
{BLUE_COLOR}==> Executing: '
BLUECOLOR==>Executing:′{@}’.${END_COLOR}”
eval $@ || exit $?
}
Install list software.
install_software() {
local install_software_list=
(“git” “vim” “tmux” “htop” “vsftpd” “openssh-server” “nfs-kernel-server” “portmap” “samba”)
echo -e “
P
I
N
K
C
O
L
O
R
i
n
s
t
a
l
l
s
o
f
t
w
a
r
e
l
i
s
t
:
{PINK_COLOR}install_software_list:
PINKCOLORinstallsoftwarelist:{install_software_list[*]}.${END_COLOR}”
#install git
if (echo "${install_software_list[@]}" | grep -wq "git");then
apt-get -y install git && echo -e "${BLUE_COLOR}git install completed.${END_COLOR}"
fi
#install and configure vim
if (echo "${install_software_list[@]}" | grep -wq "vim");then
apt-get -y install vim && vim_configure && echo -e "${BLUE_COLOR}vim install completed.${END_COLOR}"
fi
#install tmux
if (echo "${install_software_list[@]}" | grep -wq "tmux");then
apt-get -y install tmux && echo -e "${BLUE_COLOR}tmux install completed.${END_COLOR}"
fi
#install htop
if (echo "${install_software_list[@]}" | grep -wq "htop");then
apt-get -y install htop && echo -e "${BLUE_COLOR}htop install completed.${END_COLOR}"
fi
#install and configure vsftpd
if (echo "${install_software_list[@]}" | grep -wq "vsftpd");then
apt-get -y install vsftpd && ftp_configure && echo -e "${BLUE_COLOR}vsftpd install completed.${END_COLOR}"
fi
#install openssh-server
if (echo "${install_software_list[@]}" | grep -wq "openssh-server");then
apt-get -y install openssh-server && echo -e "${BLUE_COLOR}openssh-server install completed.${END_COLOR}"
fi
#install and configure nfs-kernel-server
if (echo "${install_software_list[@]}" | grep -wq "nfs-kernel-server");then
apt-get -y install nfs-kernel-server && nfs_configure && \
/etc/init.d/nfs-kernel-server restart && echo -e "${BLUE_COLOR}nfs-kernel-server install completed.${END_COLOR}"
fi
#install portmap
if (echo "${install_software_list[@]}" | grep -wq "portmap");then
apt-get -y install portmap && echo -e "${BLUE_COLOR}portmap install completed.${END_COLOR}"
fi
#install and configure samba
if (echo "${install_software_list[@]}" | grep -wq "samba");then
apt-get -y install samba && samba_configure && echo -e "${BLUE_COLOR}samba install completed.${END_COLOR}"
fi
#others
get_host_type host_release
FA_DoExec apt-get -y install \
gnupg flex bison gperf build-essential \
zip curl libc6-dev libncurses5-dev libncurses5-dev:i386 x11proto-core-dev \
libx11-dev:i386 libreadline6-dev:i386 \
libgl1-mesa-glx-lts-$host_release:i386 libgl1-mesa-dev-lts-$host_release \
g++-multilib mingw32 tofrodos libncurses5-dev:i386 \
python-markdown libxml2-utils xsltproc zlib1g-dev:i386
if [ ! -h /usr/lib/i386-linux-gnu/libGL.so ]; then
FA_DoExec ln -s /usr/lib/i386-linux-gnu/mesa/libGL.so.1 \
/usr/lib/i386-linux-gnu/libGL.so
fi
# Development support
FA_DoExec apt-get -y install \
dos2unix minicom gawk
echo -e "${GREEN_COLOR}software install completed.${END_COLOR}"
}
check_network
check_root
check_user_name
update_software_source
install_software
echo -e “
G
R
E
E
N
C
O
L
O
R
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
{GREEN_COLOR}===================================================
GREENCOLOR==================================================={END_COLOR}”
echo -e “
G
R
E
E
N
C
O
L
O
R
=
=
=
=
=
=
=
=
=
=
=
=
s
e
t
u
p
u
b
u
n
t
u
h
o
s
t
e
n
v
o
k
!
=
=
=
=
=
=
=
=
=
=
=
=
=
=
{GREEN_COLOR}============setup ubuntu host env ok!==============
GREENCOLOR============setupubuntuhostenvok!=============={END_COLOR}”
echo -e “
G
R
E
E
N
C
O
L
O
R
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
{GREEN_COLOR}===================================================
GREENCOLOR==================================================={END_COLOR}”
su $user_name
exit 0
{% endcodeblock %}
就目前来看,shell脚本还不算太难。主要就是提取某个文本内容,然后做出判断,对应执行。技巧性还是蛮多的,很有乐趣。
遇到要实现某个功能,百度一下也基本有。调试的时候,能够打印出变量,或者提出局部代码进行运行测试,还是很方便的。