Shell|脚本编写

Posted by Elli0t on 2020-08-08

写法

使用 ./test.sh 和使用 source test.sh 的区别,./test.sh 是 script 在子进程的 shell 内执行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@ubuntu] # bash test.sh
Please input your first name: yao <==输入firstname
Please input your last name: pentonBin <==输入lastname

Your full name is: yao pentonBin
[root@ubuntu] # echo $firstname
<==这里没有输出

[root@ubuntu] # source test.sh
Please input your first name: yao <==输入firstname
Please input your last name: pentonBin <==输入lastname

Your full name is: yao pentonBin
[root@ubuntu] # echo $firstname
yao <==这里输出firstname
1
2
3
4
5
# 判断是否含有i,没有就返回,有就不操作,继续执行后面的
case $- in
*i*) ;;
*) return;;
esac

./configure --prefix=/usr/local/python35的用处

里以安装python35为例,我们打算把它安装到目录/usr/local/python35,于是在python35目录执行带选项的脚本./configure –prefix=/usr/local/python35执行成功后再编译、安装(make,make install);安装完成将自动生成目录python35而且该软件所有的文件都被复制到这个目录。为什么要指定这个安装目录?是为了以后的维护方便,如果没有用这个选项,安装过程结束后,该软件所需的软件被复制到不同的系统目录下,很难弄清楚到底复制了那些文件、都复制到哪里去了—基本上是一塌糊涂。

用了—prefix选项的另一个好处是卸载软件或移植软件。当某个安装的软件不再需要时,只须简单的删除该安装目录,就可以把软件卸载得干干净净;移植软件只需拷贝整个目录到另外一个机器即可(相同的操作系统)。

案例

别人写的 shell(参考)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/bin/sh


echo "This shell need your superuser password"
echo "Don't worry,I don't want to do something bad for your virtual machine."
echo -e "run this shell with : \E[1;31msource .\\init_your_ubuntu.sh\E[0m"

case $- in
*i*) ;;
*) return;;
esac


sudo echo -e "\033[32mOriginal Setting has been started! \033[0m"
# resolving apt-get & system upgrade

# rm the lock
sudo rm /var/cache/apt/archives/lock-frontend
sudo rm /var/lib/dpkg/lock-frontend
sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/dpkg/lock

echo "deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse" | sudo tee /etc/apt/sources.list
sudo apt-get update
sudo apt-get -y upgrade

# resolving python3-pip
sudo apt-get -y install python3-pip
sudo -H python3 -m pip install -U pip -i https://pypi.tuna.tsinghua.edu.cn/simple
sudo -H python3 -m pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

#resolving python-pip
sudo apt-get -y install python-pip
sudo -H python -m pip install -U pip -i https://pypi.tuna.tsinghua.edu.cn/simple
sudo -H python -m pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

# install git
sudo apt -y install git

# install pwntools 3.12.2 in python2
sudo -H python -m pip install pwntools==3.12.2

# install pwntools(the latest version) in python3
sudo -H python3 -m pip install pwntools


# install angr
sudo apt-get -y install python3-dev libffi-dev build-essential virtualenvwrapper
sudo -H python3 -m pip install virtualenvwrapper
sudo -H python3 -m pip install setuptools

sudo -H python3 -m pip install -U virtualenv
sudo -H python3 -m pip install -U setuptools

echo "export WORKON_HOME=~/.environments" >> ~/.bashrc
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc
source ~/.bashrc
mkvirtualenv --python=$(which python3) angr && python3 -m pip install angr -i https://pypi.tuna.tsinghua.edu.cn/simple
deactivate

#ropper
sudo -H python3 -m pip install capstone
sudo -H python3 -m pip install filebytes
sudo -H python3 -m pip install ropper

#onegadget
sudo apt -y install ruby
sudo gem install one_gadget


#curl
sudo apt -y install curl

#net-tools
sudo apt -y install net-tools

#z3
sudo -H python3 -m pip install z3

#gmpy2
# m4 1.4.18
mkdir m4
cd m4
wget http://ftp.gnu.org/gnu/m4/m4-1.4.18.tar.gz
tar -zxf m4-1.4.18.tar.gz
cd m4-1.4.18
./configure -prefix=/usr/local
make
make check #FAIL: test-update-copyright.sh : can be ignore
sudo make install
cd ..
cd ..
rm -rf ./m4


# gmp 6.2.0
mkdir gmp
cd gmp
wget https://gmplib.org/download/gmp/gmp-6.2.0.tar.bz2
tar -jxvf gmp-6.2.0.tar.bz2 && cd gmp-6.2.0
./configure --prefix=$HOME/static --enable-static --disable-shared --with-pic
make
make check
make install
cd ..
cd ..
rm -rf ./gmp


# MPFR 4.0.2
mkdir mpfr
cd mpfr
wget http://www.mpfr.org/mpfr-current/mpfr-4.0.2.tar.xz
tar xf mpfr-4.0.2.tar.xz && cd mpfr-4.0.2
./configure --prefix=$HOME/static --enable-static --disable-shared --with-pic --with-gmp=$HOME/static
make
make check
make install
cd ..
cd ..
rm -rf mpfr

# MPC 1.1.0
mkdir MPC
cd MPC
wget ftp://ftp.gnu.org/gnu/mpc/mpc-1.1.0.tar.gz
tar -zxvf mpc-1.1.0.tar.gz && cd mpc-1.1.0
./configure --prefix=$HOME/static --enable-static --disable-shared --with-pic --with-gmp=$HOME/static --with-mpfr=$HOME/static
make
make check
make install
cd ..
cd ..
rm -rf MPC

# complie gmpy2 2.0.8
mkdir gmpy2
cd gmpy2
wget https://files.pythonhosted.org/packages/90/f4/9a2e384b325b69bc5827b9a6510a8fb4a51698c915c06a3f25a86458892a/gmpy2-2.0.8.zip
unzip gmpy2-2.0.8.zip && cd gmpy2-2.0.8
sudo -H python3 setup.py build_ext --static-dir=$HOME/static install
cd ..
cd ..
rm -rf ./gmpy2

# result
echo
echo
echo -e "\033[32mOriginal Setting has been finished! \033[0m\n"
echo -e "\033[32mEnjoy! \033[0m\n"
echo "installed:"
echo " python3-pip"
echo " python2-pip"
echo " git"
echo " OneGadget"
echo " curl"
echo " net-tools"
echo " m4 1.4.18"
echo " gmp 6.2.0"
echo " MPFR 4.0.2"
echo " MPC 1.1.0"
echo -e " pwntools 3.12.2 \033[33m in python2 \033[0m"
echo -e " pwntools(the latest version) \033[36m in python3 \033[0m"
echo -e " ropper \033[36m in python3 \033[0m"
echo -e " angr \033[36m in python3 \033[0m"
echo -e " gmpy2 \033[36m in python3 \033[0m"
echo -e " z3 2.0.8 \033[36m in python3 \033[0m"
echo

我写的 shell(有些错误)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/bin/sh

case $- in
*i*) ;;
*) return;;
esac

sudo yum clean up
sudo yum makecache
sudo yum update
echo -e "\033[32mUpdate is ovel \033[0m\n"
sudo yum install -y percona-xtrabackup-80-8.0.9-1.el7.x86_64.rpm
sudo yum install -y qpress-11-1.el7.x86_64.rpm
echo -e "\033[32mxtrabackup is installed \033[0m\n"
wget https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm
sudo yum -y localinstall mysql80-community-release-el7-1.noarch.rpm
sudo yum -y install yum-utils
sudo yum -y install mysql-community-server
sudo cat xxxxx_qp.xb |sudo xbstream -x -v -C /var/lib/mysql
sudo xtrabackup --decompress --remove-original --target-dir=/var/lib/mysql
sudo xtrabackup --prepare --apply-log-only --target-dir=/var/lib/mysql
sudo chown -R mysql.mysql /var/lib/mysql
sudo rm -rf /etc/my.cnf

echo "[mysqld]
lower_case_table_names=1
innodb_checksum_algorithm=crc32
innodb_data_file_path=ibdata1:200M:autoextend
innodb_log_files_in_group=2
innodb_log_file_size=524288000
innodb_undo_directory=/var/lib/mysql/
basedir=/usr
datadir=/var/lib/mysql
innodb_undo_tablespaces=0
server_id=999098802
skip-grant-tables=1
sql_mode=''" | sudo tee /etc/my.cnf

sudo systemctl start mysqld.service
mysql -u –p -e "flush privileges"
mysql -u –p -e "alter user 'aliyun_root'@'127.0.0.1' identified by '123456'"
mysql -u –p -e "flush privileges"
sudo rm -rf /etc/my.cnf

echo "[mysqld]
lower_case_table_names=1
innodb_checksum_algorithm=crc32
innodb_data_file_path=ibdata1:200M:autoextend
innodb_log_files_in_group=2
innodb_log_file_size=524288000
innodb_undo_directory=/var/lib/mysql/
basedir=/usr
datadir=/var/lib/mysql
innodb_undo_tablespaces=0
server_id=999098802
sql_mode=''" | sudo tee /etc/my.cnf

sudo systemctl restart mysqld.service
echo -e "\033[32mall ready done !\033[0m\n"
echo -e "please enter \033[32m mysql -ualiyun_root –p123456 -h127.0.0.1' to start ! \033[0m"

参考链接

https://blog.csdn.net/RonnyJiang/article/details/53283258?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.compare&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.compare