Linux实验总结
学院: 专业: 姓名: 学号:
我在Linux操作系统下主要学习了shell脚本编程。我在实验中基本掌握shell脚本语言的编程规范。能够看懂大部分shell脚本语言,能够独立编写一些简单的shell脚本程序。
首先我来讲一讲我对shell的理解。Shell是一个命令解释器,它解释由用户输入的命令并且把它们送到内核。不仅如此,Shell有自己的编程语言用于对命令的编辑,它允许用户编写由shell命令组成的程序。Shell可以将用户操作的Linux集合起来,类似于matlab的M文件一样。是一种解释性的语言。使用shell往往可以简化一些操作,少输入一些指令。
实验1--------我自己编了一个shell程序,来简化编程时,繁琐的使用gcc编译的源代码。
conbind.sh
#!/bin/sh
cd Documents/socket
gcc -o Fileclient File_client.c
gcc -o Filesever File_server.c
通过这个shell程序,我在使用网络编程的时候,修改代码的时候重新编译,就可以使用shell直接编译源代码。输入./conbind.sh就可以直接将我两条复杂的指令一次性执行。
在写完shell脚本之后,需要执行指令。在执行shell的时候就涉及chmod指令。
chmod----改变一个或多个文件的存取模式(mode)。
chmod 具体使用规则如下:
chmod [who] operator [permission] filename
who的含义是:
u 文件属主权限。
g 属组用户权限。
o 其他用户权限。
a 所有用户(文件属主、属组用户及其他用户)。
operator的含义:
+ 增加权限。
- 取消权限。
= 设定权限。
permission的含义:
r 读权限。
w 写权限。
x 执行权限。
s 文件属主和组set-ID。
t 粘性位*。
l 给文件加锁,使其他用户无法访问。
u,g,o 针对文件属主、属组用户及其他用户的操作。
举例如下:
chmod a-x temp //rw- rw- rw- 收回所有用户的执行权限
chmod og-w temp //rw- r-- r- - 收回属组用户和其他用户的写权限
chmod g+w temp //rw- rw- r- - 赋予属组用户写权限
chmod u+x temp //rwx rw- r- - 赋予文件属主执行权限
chmod go+x temp //rwx rwx r- x 赋予属组用户和其他用户执行权限
2.实验二----------用shell实现一个循环创建文件夹
#!/bin/bash //这里用的解释器是bash
echo hello
i=1
while((i<5))
do mkdir hello$i
i=$(($i+1))
done
用bash解释器的好处是语法规则类似于c语言。 Shell解释器的作用:shell解释器的作用就是对用户输入的命令进行“解释”,有了它,用户才可以在 linux 系统中任意挥洒。没有它的帮助,你纵然十八般本领在身,也施展不出。
bash每次在“解释”完用户命令之后,又打印出一行提示符,然后继续等待用户的下一个命令。这种循环式的设计,使得用户可以始终处于 bash 的控制之下。除非你输入 exit、logout明确表示要退出 bash。
3.实验三-----------用shell实现读取当前用户名,文件位置
#!/bin/bash
echo your name is
whoami
echo your position is
pwd
这个充分体现了shell脚本的优越性,将多指令集合在一个脚本文件里。一次性执行,提高了执行效率。方便用户使用。尤其在大的项目中优势明显。
4.实验四-------------用shell配置服务器////代码非原创!!!
每次新装服务器后,总要配置一大堆东西,我们可以通过shell自动修改主机名,网关,双网卡绑定(包括重置IP地址功能),重启网络,自动配置ntp时间同步服务,自动配置dns服务。由于修改主机名需要重启生效,所以在最后加了重启选项。当所有配置完毕,可以按7完成重启。
这个代码是我在网上找来的,因为我之前要搭建一个Linux服务器。所以在配置服务器的过程中遇到了很多问题。Shell脚本语言的基本规则我已经掌握,但是还没有能力独立编写这么复杂的程序。我会在之后的学习过程中深入了解shell,达到这个水准。 1.
2.
3.
4.
5.
6. #!/bin/bash #Initialize server #write by xiaojing.zhao #2013.1.5 menu()
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. { clear scriptname="autocfg.sh" version=1.2.0 date=`date +%F.%T` cat <<MENULIST ====================================================================== ScriptName:$scriptname Version:$version Date&Time:$ ======================================================================This shell script can automatically complete the following configurati 1.modify hostname 2.modify gateway 3.configure bond0 for eth0 & eth1 4.restart active network service 5.configure ntp client 6.configure dns service 7.reboot host ====================================================================== MENULIST echo -n "Please input your choice [1,2,3,4,5,6,7(anykey),b(back),q(quiread choice } hw_eth0=`ifconfig eth0 | grep -i hwaddr | awk '{print $5}'` hw_eth1=`ifconfig eth1 | grep -i hwaddr | awk '{print $5}'` ######################################################################get_ipaddr() { ============== date ============== on: ============== t),a(all)]:" ##############
44.
45. ipaddra=`ifconfig -a | grep inet | grep -v inet6 | grep -v 127 #nw=`ifconfig -a | grep inet | grep -v inet6 | grep -v 127.0.0.0.0.1 | awk '{print $2}'`
.1 | awk '{print $2}' | awk -F : '{print $2}' | awk -F . '{print $1"."$2"."$3"."0}'`
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. echo "IPADDR=${ipaddra#*:}" >/etc/sysconfig/network-scripts/if for i in tmpip bond0 eth0 eth1 do ipaddrb=`grep ^IPADDR /etc/sysconfig/network-scripts/i if [ ! "${ipaddrb#*=}" = "" ] then ipaddr=${ipaddrb#*=} break fi done rm -f /etc/sysconfig/network-scripts/ifcfg-tmpip 2>/dev/null if [ "$ipaddr" = "" ] then echo "This hosts has not a valid ip address" while [ "$ipaddr" = "" ] do echo -n "Please input the address:" read ipaddr done else echo -n "Please input the address[$ipaddr]:" read ipaddr1 if [ ! "$ipaddr1" = "" ] then ipaddr=$ipaddr1 fi fi nw=`echo $ipaddr | awk -F . '{print $1"."$2"."$3"."0}'` gw=`echo $ipaddr | awk -F . '{print $1"."$2"."$3"."1}'` } ######################################################################get_brcast() { brcasta=`ifconfig -a | grep inet | grep -v inet6 | grep -v 127cfg-tmpip fcfg-$i 2>/dev/null` ##############
.0.0.1 | awk '{print $3}'`
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. echo "BROADCAST=${brcasta#*:}" >>/etc/sysconfig/network-script for i in tmpip bond0 eth0 eth1 do brcastb=`grep ^BROADCAST /etc/sysconfig/network-script if [ ! "${brcastb#*=}" = "" ] then brcast=${brcastb#*=} break fi done rm -f /etc/sysconfig/network-scripts/ifcfg-tmpip 2>/dev/null if [ "$brcast" = "" ] then echo "This hosts has not a valid broadcast" while [ "$brcast" = "" ] do echo -n "Please input the brdcast:" read brcast done else echo -n "Please input the brdcast[$brcast]:" read brcast1 if [ ! "$brcast1" = "" ] then brcast=$brcast1 fi fi } ######################################################################get_ntmask() { ntmaska=`ifconfig -a | grep inet | grep -v inet6 | grep -v 127. echo "NETMASK=${ntmaska#*:}" >>/etc/sysconfig/network-scripts/ for i in tmpip bond0 eth0 eth1 do ntmaskb=`grep ^NETMASK /etc/sysconfig/network-scripts/ if [ ! "${ntmaskb#*=}" = "" ] then s/ifcfg-tmpip s/ifcfg-$i 2>/dev/null` ############## 0.0.1 | awk '{print $4}'` ifcfg-tmpip ifcfg-$i 2>/dev/null`
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. ntmask=${ntmaskb#*=} break fi done rm -f /etc/sysconfig/network-scripts/ifcfg-tmpip 2>/dev/null if [ "$ntmask" = "" ] then echo "This hosts has not a valid ip netmask" while [ "$ntmask" = "" ] do echo -n "Please input the netmask:" read ntmask done else echo -n "Please input the netmask[$ntmask]:" read ntmask1 if [ ! "$ntmask1" = "" ] then ntmask=$ntmask1 fi fi } ######################################################################gateway() { echo -n "Do you want to configure GATEWAY?[y|n]" read myselect if [[ "$myselect" = "y" || "$myselect" = "Y" ]] then unset myselect clear orifile=/etc/sysconfig/network newfile=/root/network if ! grep GATEWAY $orifile >/dev/null then while [ "$gw" = "" ] do echo -n "Please input GATEWAY:" read gw done else echo -n "`grep GATEWAY $orifile`,Please input new GATE##############
WAY:"
160.
161.
162.
163.
164.
165.
166.
167.
e
168.
169.
170.
171.
172.
173.
174.
175.
176.
177.
178.
179.
180.
181.
182.
183.
184.
185.
186.
187.
188.
189. read gw fi if [ ! "$gw" = "" ] then #sed "/GATEWAY/d" $orifile | sed "$ a\GATEWAY=$gw" >$ #the bellow line is better,more fast (sed "/GATEWAY/d" $orifile;echo GATEWAY=$gw) >$newfil cp $newfile $orifile fi echo -e "\nThe new $orifile file's content:\n" cat $orifile echo fi echo } ######################################################################hosts() { echo -n "Do you want to configure /etc/hosts file?[y|n]" read myselect if [[ "$myselect" = "y" || "$myselect" = "Y" ]] then unset myselect clear get_ipaddr cp /etc/hosts /root/hosts.`date +%F-%T` egrep -v '^([0-9]|[0-9][0-9]|1[0-9][0-newfile ##############
9]|2[0-4][0-9]|25[0-5])\.([0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([2-9]|[0-9][0-9]|1
[0-9][0-9]|2[0-4][0-9]|25[0-5])' /etc/hosts >/root/hosts.txt
190.
191.
192.
193.
194.
195. mv /root/hosts.txt /etc/hosts echo -n "Please input your hostname[eg read line_hostname hname=`echo $line_hostname | awk -F . '{print $1}'` sed "/$ipaddr/d" /etc/hosts | sed "$ a\\$ipaddr cp /root/hosts /etc/hosts :.cn]:" $line_hostname $hname" >/root/hosts
196. 197. 198. 199. 200. 201. 202. 203. 204. 205. 206. 207. 208. 209. 210. 211. 212. 213. 214. 215. 216. 217. 218. 219. 220. 221. 222. 223. 224. 225. 226. 227. 228. 229. 230. 231. 232.
233. sed -i "/HOSTNAME/c\HOSTNAME=$line_hostname" /etc/sysc echo -e "\nThe new /etc/hosts file's content:\n" cat /etc/hosts echo fi echo } ######################################################################bond() { echo -n "Do you want to configure modprobe.conf for bonding?[y|n]" read myselect if [[ "$myselect" = "y" || "$myselect" = "Y" ]] then unset myselect clear echo -e "========== begin to modify /etc/modprobe.conf ======= filename=/etc/modprobe.conf newfile=/root/modprobe.conf #cmdfile=/tmp/change_modprobe.conf.sh grep bond0 /etc/modprobe.conf >/dev/null if [ $? = 1 ] then echo -n "Please input the bonding mode,default is 1.[1 read bmode if [ "$bmode" = "" ];then bmode=1;fi cat $filename >$newfile echo "alias bond0 bonding" >>$newfile echo "options bond0 miimon=100 mode=1" >>$newfile mv $filename $filename.`date +%F-%T` sed "s/mode=1/mode=$bmode/" $newfile >$filename echo "$filename has been modified." echo "The original file is bakup to $filename.`date +% else onfig/network ############## ====\n" |0]" F`..."
234. 235. 236. 237. 238. 239. 240. 241. 242. 243. 244. 245. 246. 247. 248. 249. 250. 251. 252. 253. 254. 255. 256. 257. 258. 259. 260. 261. 262. 263. 264. 265. 266. 267. 268. 269. 270. 271. 272. 273. 274.
275. echo "This hosts has been using bonding mode,nothing t echo "The $filename file's content:" cat /etc/modprobe.conf fi echo echo orieth0=/etc/sysconfig/network-scripts/ifcfg-eth0 orieth1=/etc/sysconfig/network-scripts/ifcfg-eth1 oribond=/etc/sysconfig/network-scripts/ifcfg-bond0 neweth0=/root/ifcfg-eth0 neweth1=/root/ifcfg-eth1 newbond=/root/ifcfg-bond0 if [ -f $oribond ] then cp $oribond $newbond.`date +%F-%T` fi if [[ ! ( ! "$ipaddr" = "" && ! "$brcast" = "" && ! "$netmask" then get_ipaddr get_brcast get_ntmask fi echo "DEVICE=bond0" >$oribond echo "BOOTPROTO=none" >>$oribond echo "ONBOOT=yes" >>$oribond echo "IPV6INIT=no" >>$oribond echo "TYPE=Ethernet" >>$oribond echo "PEERDNS=yes" >>$oribond echo "USERCTL=no" >>$oribond echo "IPADDR=$ipaddr" >>$oribond echo "BROADCAST=$brcast" >>$oribond echo "NETMASK=$ntmask" >>$oribond echo "GATEWAY=$gw" >>$oribond echo "NETWORK=$nw" >>$oribond if [ -f $orieth0 ] then cp $orieth0 $neweth0.`date +%F-%T` fi if [ -f $orieth1 ] o changed." = "" ) ]]
276. 277. 278. 279. 280. 281. 282. 283. 284. 285. 286. 287. 288. 289. 290. 291. 292. 293. 294. 295. 296. 297. 298. 299. 300. 301. 302. 303. 304. 305. 306. 307. 308. 309. 310. 311. 312. 313. 314. 315. 316. 317.
318. then cp $orieth1 $neweth1.`date +%F-%T` fi echo "DEVICE=eth0" >$orieth0 echo "BOOTPROTO=none" >>$orieth0 echo "MASTER=bond0" >>$orieth0 echo "SLAVE=yes" >>$orieth0 echo "ONBOOT=yes" >>$orieth0 echo "HWADDR=$hw_eth0" >>$orieth0 #sed -i '/HWADDR/c\HWADDR='$hw_eth0'' $orieth0 echo "IPV6INIT=no" >>$orieth0 echo "TYPE=Ethernet" >>$orieth0 echo "PEERDNS=yes" >>$orieth0 echo "USERCTL=no" >>$orieth0 echo "DEVICE=eth1" >$orieth1 echo "BOOTPROTO=none" >>$orieth1 echo "MASTER=bond0" >>$orieth1 echo "SLAVE=yes" >>$orieth1 echo "ONBOOT=yes" >>$orieth1 echo "HWADDR=$hw_eth1" >>$orieth1 #sed -i '/HWADDR/c\HWADDR='$hw_eth1'' $orieth1 echo "IPV6INIT=no" >>$orieth1 echo "TYPE=Ethernet" >>$orieth1 echo "PEERDNS=yes" >>$orieth1 echo "USERCTL=no" >>$orieth1 echo -e "\ncat $oribond" cat $oribond echo -e "\ncat $orieth0" cat $orieth0 echo -e "\ncat $orieth1" cat $orieth1 echo fi echo } ######################################################################ntp() ##############
319. 320. 321. 322. 323. 324. 325. 326. 327. 328. 329. 330. 331. 332. 333. 334. 335. 336. 337. 338. 339. 340. 341. 342. 343. 344. 345. 346. 347. 348. 349. 350. 351. 352. 353. 354. 355. 356. 357. 358.
359. { echo -n "Do you want to configure ntp client?[y|n]" read myselect if [[ "$myselect" = "y" || "$myselect" = "Y" ]] then unset myselect clear orifile=/var/spool/cron/root newfile=/root/root if [ -f $orifile ] then sed "/ntpdate/d" $orifile > $newfile else touch $orifile fi if grep ntpdate $orifile > /dev/null then for i in `sed -n '/ntpdate/p' $orifile | awk '{print $7}'` do ((num++)) echo -n "Please input the new ntp server ip-addr. serv #read ntpip[$num] read ntpip if [ "$ntpip" = "" ] then ntpip=$i fi echo "$num * * * * /usr/sbin/ntpdate $ntpip" >> $newfi done unset num else echo "You can assign max two ntp server's ip!" echo for ((i=0;i<2;i++)) do echo -n "Please input the new ntp server ip-addr:" read ntpip if [ ! "$ntpip" = "" ] then echo "$i * * * * /usr/sbin/ntpdate $ntpip" >> fi er $num's ip [$i]:" le $newfile
360. 361. 362. 363. 364. 365. 366. 367. 368. 369. 370. 371. 372. 373. 374. 375. 376. 377. 378. 379. 380. 381. 382. 383. 384. 385. 386. 387. 388. 389. 390. 391. 392. 393. 394. 395. 396. 397. 398. 399.
400. done fi cp $newfile $orifile echo echo "The new root's crontab is" crontab -l /usr/sbin/ntpdate $ntpip echo echo fi echo } ######################################################################dns() { echo -n "Do you want to configure dns service?[y|n]" read myselect if [[ "$myselect" = "y" || "$myselect" = "Y" ]] then unset myselect clear echo -e "You can assign max two dns server's ip!\n" for ((i=0;i<2;i++)) do echo -n "Please input the new dns server ip-ad read dnsip echo "nameserver "$dnsip >> /etc/resolv.conf done fi echo } ######################################################################autostart() { echo -n "Do you want to active network now?[y|n]" read myselect if [[ "$myselect" = "y" || "$myselect" = "Y" ]] then unset myselect clear service network restart >/dev/null 2>&1 ############## dr:" ##############
401. 402. 403. 404. 405. 406. 407. 408. 409. 410. 411. 412. 413. 414. 415. 416. 417. 418. 419. 420. 421. 422. 423. 424. 425. 426. 427. 428. 429. 430. 431. 432. 433. 434. 435. 436. 437. 438. 439. 440. 441. 442.
443. service network restart fi echo } ######################################################################for ((j=1;;j++)) do menu case "$choice" in "1") hosts ;; "2") gateway ;; "3") bond ;; "4") autostart ;; "5") ntp ;; "6") dns ;; "7") reboot ;; "8") ;; "a") gateway hosts bond ntp dns autostart exit 0 ;; "b") unset choice ##############
444. 445. 446. 447. 448. 449. 450. 451. 452. 453. 454. ;; "q") exit 0 ;; esac if [ ! "$choice" = "" ] then echo "Press any key to return!" read fi done