Touch创建.sh文件,vim进行编辑,echo写语句, ls-l查看权限,chmod更改权限,cd切换到该存储路径,.文件名 进行显示。
[root@localhost ~]# cd /home
[root@localhost home]# touch kkk.sh
[root@localhost home]# vim kkk.sh
#!/bin/sh
echo -e "input"
read user password
echo "user=$user"
echo "password=$password
[root@localhost home]# ./kkk.sh
[root@localhost home]# ls -l kkk.sh
-rw-r--r-- 1 root root 91 06-23 22:33 kkk.sh
[root@localhost home]# chmod 744 kkk.sh
[root@localhost home]# ls -l kkk.sh
-rwxr--r-- 1 root root 91 06-23 22:33 kkk.sh
[root@localhost home]# ./kkk.sh
input
15542 545
user=15542
password=545
top ps –ef tee 三联用
[root@localhost home]# top
top - 22:38:25 up 3:24, 2 users, load average: 0.00, 0.00, 0.00
Tasks: 160 total, 3 running, 156 sleeping, 0 stopped, 1 zombie
Cpu(s): 0.0%us, 0.0%sy, 0.0%ni, 99.0%id, 0.0%wa, 0.0%hi, 1.0%si, 0.0%st
Mem: 1035108k total, 963856k used, 71252k free, 55816k buffers
Swap: 2097144k total, 0k used, 2097144k free, 713292k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
2933 root 15 0 9920 2828 2272 R 0.3 0.3 0:00.21 sshd
3063 root 15 0 39448 4056 3312 R 0.3 0.4 0:02.85 vmtoolsd
1 root 15 0 2072 636 544 S 0.0 0.1 0:00.76 init
2 root RT -5 0 0 0 S 0.0 0.0 0:00.00 migration/0
3 root 34 19 0 0 0 S 0.0 0.0 0:00.00 ksoftirqd/0
4 root RT -5 0 0 0 S 0.0 0.0 0:00.00 watchdog/0
5 root 10 -5 0 0 0 S 0.0 0.0 0:00.04 events/0
6 root 10 -5 0 0 0 S 0.0 0.0 0:00.00 khelper
7 root 10 -5 0 0 0 S 0.0 0.0 0:00.00 kthread
10 root 10 -5 0 0 0 S 0.0 0.0 0:00.69 kblockd/0
11 root 20 -5 0 0 0 S 0.0 0.0 0:00.00 kacpid
177 root 18 -5 0 0 0 S 0.0 0.0 0:00.00 cqueue/0
180 root 10 -5 0 0 0 S 0.0 0.0 0:00.00 khubd
182 root 10 -5 0 0 0 S 0.0 0.0 0:00.00 kseriod
248 root 15 0 0 0 0 S 0.0 0.0 0:00.00 khungtaskd
249 root 23 0 0 0 0 S 0.0 0.0 0:00.00 pdflush
250 root 15 0 0 0 0 S 0.0 0.0 0:01.30 pdflush
[root@localhost home]# vim kk1。txt
[root@localhost home]# ps -ef |tee -a kk1.txt
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 19:14 ? 00:00:00 init [5]
root 2 1 0 19:14 ? 00:00:00 [migration/0]
root 3 1 0 19:14 ? 00:00:00 [ksoftirqd/0]
root 4 1 0 19:14 ? 00:00:00 [watchdog/0]
root 5 1 0 19:14 ? 00:00:00 [events/0]
root 6 1 0 19:14 ? 00:00:00 [khelper]
root 7 1 0 19:14 ? 00:00:00 [kthread]
root 10 7 0 19:14 ? 00:00:00 [kblockd/0]
root 11 7 0 19:14 ? 00:00:00 [kacpid]
root 177 7 0 19:14 ? 00:00:00 [cqueue/0]
root 180 7 0 19:14 ? 00:00:00 [khubd]
root 182 7 0 19:14 ? 00:00:00 [kseriod]
root 248 7 0 19:14 ? 00:00:00 [khungtaskd]
root 249 7 0 19:14 ? 00:00:00 [pdflush]
root 250 7 0 19:14 ? 00:00:01 [pdflush]
root 251 7 0 19:14 ? 00:00:00 [kswapd0]
root 252 7 0 19:14 ? 00:00:00 [aio/0]
root 469 7 0 19:14 ? 00:00:00 [kpsmoused]
root 499 7 0 19:14 ? 00:00:00 [mpt_poll_0]
root 500 7 0 19:14 ? 00:00:00 [mpt/0]
root 501 7 0 19:14 ? 00:00:00 [scsi_eh_0]
root 504 7 0 19:14 ? 00:00:00 [ata/0]
root 505 7 0 19:14 ? 00:00:00 [ata_aux]
root 510 7 0 19:14 ? 00:00:00 [scsi_eh_1]
root 511 7 0 19:14 ? 00:00:03 [scsi_eh_2]
root 512 7 0 19:14 ? 00:00:00 [scsi_eh_3]
root 513 7 0 19:14 ? 00:00:00 [scsi_eh_4]
root 514 7 0 19:14 ? 00:00:00 [scsi_eh_5]
root 515 7 0 19:14 ? 00:00:00 [scsi_eh_6]
root 516 7 0 19:14 ? 00:00:00 [scsi_eh_7]
root 517 7 0 19:14 ? 00:00:00 [scsi_eh_8]
root 518 7 0 19:14 ? 00:00:00 [scsi_eh_9]
root 519 7 0 19:14 ? 00:00:00 [scsi_eh_10]
root 520 7 0 19:14 ? 00:00:00 [scsi_eh_11]
root 521 7 0 19:14 ? 00:00:00 [scsi_eh_12]
root 522 7 0 19:14 ? 00:00:00 [scsi_eh_13]
root 523 7 0 19:14 ? 00:00:00 [scsi_eh_14]
root 524 7 0 19:14 ? 00:00:00 [scsi_eh_15]
root 525 7 0 19:14 ? 00:00:00 [scsi_eh_16]
root 526 7 0 19:14 ? 00:00:00 [scsi_eh_17]
root 527 7 0 19:14 ? 00:00:00 [scsi_eh_18]
root 528 7 0 19:14 ? 00:00:00 [scsi_eh_19]
root 529 7 0 19:14 ? 00:00:00 [scsi_eh_20]
root 530 7 0 19:14 ? 00:00:00 [scsi_eh_21]
root 531 7 0 19:14 ? 00:00:00 [scsi_eh_22]
root 532 7 0 19:14 ? 00:00:00 [scsi_eh_23]
root 533 7 0 19:14 ? 00:00:00 [scsi_eh_24]
root 534 7 0 19:14 ? 00:00:00 [scsi_eh_25]
root 535 7 0 19:14 ? 00:00:00 [scsi_eh_26]
root 536 7 0 19:14 ? 00:00:00 [scsi_eh_27]
root 537 7 0 19:14 ? 00:00:00 [scsi_eh_28]
root 538 7 0 19:14 ? 00:00:00 [scsi_eh_29]
root 539 7 0 19:14 ? 00:00:00 [scsi_eh_30]
root 542 7 0 19:14 ? 00:00:00 [kstriped]
root 551 7 0 19:14 ? 00:00:00 [ksnapd]
root 568 7 0 19:14 ? 00:00:05 [kjournald]
root 594 7 0 19:15 ? 00:00:00 [kauditd]
root 627 1 0 19:15 ? 00:00:00 /sbin/udevd -d
root 1379 7 0 19:15 ? 00:00:00 [kgameportd]
root 1901 1 0 21:41 ? 00:00:00 /usr/local/apache2/bin/httpd -k start
daemon 1902 1901 0 21:41 ? 00:00:00 /usr/local/apache2/bin/httpd -k start
daemon 1903 1901 0 21:41 ? 00:00:00 /usr/local/apache2/bin/httpd -k start
daemon 1904 1901 0 21:41 ? 00:00:00 /usr/local/apache2/bin/httpd -k start
daemon 1905 1901 0 21:41 ? 00:00:00 /usr/local/apache2/bin/httpd -k start
daemon 1906 1901 0 21:41 ? 00:00:00 /usr/local/apache2/bin/httpd -k start
daemon 2000 1901 0 21:45 ? 00:00:00 /usr/local/apache2/bin/httpd -k start
root 2267 7 0 19:15 ? 00:00:00 [kmpathd/0]
root 2268 7 0 19:15 ? 00:00:00 [kmpath_handlerd]
root 2327 7 0 19:15 ? 00:00:00 [kjournald]
root 2749 7 0 19:15 ? 00:00:00 [vmmemctl]
root 2933 3765 0 22:27 ? 00:00:00 sshd: root@pts/1
root 2939 2933 0 22:27 pts/1 00:00:00 -bash
root 3063 1 0 19:15 ? 00:00:02 /usr/sbin/vmtoolsd
root 3182 2939 0 22:37 pts/1 00:00:00 top
root 3265 2939 0 22:42 pts/1 00:00:00 ps -ef
root 3266 2939 0 22:42 pts/1 00:00:00 -bash
root 3372 1 0 19:15 ? 00:00:00 /sbin/dhclient -1 -q -lf /var/lib/dhclient/dhclient-eth0.leases -pf /var/run/dhclient-eth0.pid eth0
root 3417 1 0 19:15 ? 00:00:00 auditd
root 3419 3417 0 19:15 ? 00:00:00 /sbin/audispd
root 3440 1 0 19:15 ? 00:00:00 syslogd -m 0
root 3444 1 0 19:15 ? 00:00:00 klogd -x
rpc 3496 1 0 19:15 ? 00:00:00 portmap
root 3522 7 0 19:15 ? 00:00:00 [rpciod/0]
root 3528 1 0 19:15 ? 00:00:00 rpc.statd
root 3552 1 0 19:15 ? 00:00:00 rpc.idmapd
dbus 3567 1 0 19:15 ? 00:00:00 dbus-daemon --system
root 3582 1 0 19:15 ? 00:00:00 /usr/sbin/hcid
root 3586 1 0 19:15 ? 00:00:00 /usr/sbin/sdpd
root 3609 1 0 19:15 ? 00:00:00 [krfcommd]
root 3644 1 0 19:15 ? 00:00:00 pcscd
root 3654 1 0 19:15 ? 00:00:00 /usr/sbin/acpid
68 3667 1 0 19:15 ? 00:00:02 hald
root 3668 3667 0 19:15 ? 00:00:00 hald-runner
68 3684 3668 0 19:16 ? 00:00:00 hald-addon-keyboard: listening on /dev/input/event0
root 3693 3668 0 19:16 ? 00:00:03 hald-addon-storage: polling /dev/scd0
68 3697 3668 0 19:16 ? 00:00:00 hald-addon-acpi: listening on acpid socket /var/run/acpid.socket
root 3710 1 0 19:16 ? 00:00:00 /usr/bin/hidd --server
root 3730 1 0 19:16 ? 00:00:00 automount
root 3747 1 0 19:16 ? 00:00:00 ./hpiod
root 3752 1 0 19:16 ? 00:00:00 python ./hpssd.py
root 3765 1 0 19:16 ? 00:00:00 /usr/sbin/sshd
root 3774 1 0 19:16 ? 00:00:00 cupsd
root 3788 1 0 19:16 ? 00:00:00 xinetd -stayalive -pidfile /var/run/xinetd.pid
root 3847 1 0 19:16 ? 00:00:00 tpvmlpd2
root 3881 1 0 19:16 ? 00:00:00 sendmail: accepting connections
smmsp 3891 1 0 19:16 ? 00:00:00 sendmail: Queue runner@01:00:00 for /var/spool/clientmqueue
root 3904 1 0 19:16 ? 00:00:00 gpm -m /dev/input/mice -t exps2
root 3919 1 0 19:16 ? 00:00:00 crond
xfs 3979 1 0 19:16 ? 00:00:00 xfs -droppriv -daemon
root 3996 1 0 19:16 ? 00:00:00 /usr/sbin/atd
avahi 4026 1 0 19:16 ? 00:00:00 avahi-daemon: running [localhost.local]
avahi 4027 4026 0 19:16 ? 00:00:00 avahi-daemon: chroot helper
root 4111 1 0 19:16 ? 00:00:00 /usr/sbin/smartd -q never
root 4118 1 0 19:16 tty1 00:00:00 /sbin/mingetty tty1
root 4126 1 0 19:16 tty2 00:00:00 /sbin/mingetty tty2
root 4129 1 0 19:16 tty3 00:00:00 /sbin/mingetty tty3
root 4132 1 0 19:16 tty4 00:00:00 /sbin/mingetty tty4
root 4133 1 0 19:16 tty5 00:00:00 /sbin/mingetty tty5
root 4134 1 0 19:16 tty6 00:00:00 /sbin/mingetty tty6
root 4167 1 0 19:16 ? 00:00:00 /usr/sbin/gdm-binary -nodaemon
root 4221 4167 0 19:16 ? 00:00:00 /usr/sbin/gdm-binary -nodaemon
root 4223 1 0 19:16 ? 00:00:00 /usr/libexec/gdm-rh-security-token-helper
root 4226 4221 0 19:16 tty7 00:00:59 /usr/bin/Xorg :0 -br -audit 0 -auth /var/gdm/:0.Xauth -nolisten tcp vt7
root 4249 1 0 19:17 ? 00:00:00 /usr/bin/python -tt /usr/sbin/yum-updatesd
root 4251 1 0 19:17 ? 00:00:00 /usr/libexec/gam_server
root 4252 4221 0 19:17 ? 00:00:00 /usr/bin/gnome-session
root 4284 4252 0 19:17 ? 00:00:00 [Xsession]
root 4287 4252 0 19:17 ? 00:00:00 /usr/bin/ssh-agent /bin/sh -c exec -l /bin/bash -c "/usr/bin/dbus-launch --exit-with-session /etc/X11/xinit/Xclients"
root 4316 1 0 19:17 ? 00:00:00 /usr/bin/dbus-launch --exit-with-session /etc/X11/xinit/Xclients
root 4317 1 0 19:17 ? 00:00:00 /bin/dbus-daemon --fork --print-pid 4 --print-address 6 --session
root 4323 1 0 19:17 ? 00:00:00 /usr/libexec/gconfd-2 5
root 4328 1 0 19:17 ? 00:00:00 /usr/bin/gnome-keyring-daemon
root 4330 1 0 19:17 ? 00:00:00 /usr/libexec/gnome-settings-daemon
root 4350 1 0 19:17 ? 00:00:05 metacity --sm-client-id=default1
root 4353 1 0 19:17 ? 00:00:00 /usr/lib/scim-1.0/scim-launcher -d -c simple -e all -f socket --no-stay
root 4356 1 0 19:17 ? 00:00:01 gnome-panel --sm-client-id default2
root 4358 1 0 19:17 ? 00:00:13 nautilus --no-default-window --sm-client-id default3
root 4362 1 0 19:17 ? 00:00:00 /usr/libexec/bonobo-activation-server --ac-activate --ior-output-fd=16
root 4365 1 0 19:17 ? 00:00:00 /usr/libexec/gnome-vfs-daemon
root 4370 1 0 19:17 ? 00:00:00 eggcups --sm-client-id default4
root 4373 1 0 19:17 ? 00:00:00 gnome-volume-manager --sm-client-id default5
root 4379 1 0 19:17 ? 00:00:06 /usr/lib/vmware-tools/sbin32/vmtoolsd -n vmusr --blockFd 3
root 4392 1 0 19:17 ? 00:00:00 /usr/libexec/mapping-daemon
root 4403 1 0 19:17 ? 00:00:03 /usr/libexec/wnck-applet --oaf-activate-iid=OAFIID:GNOME_Wncklet_Factory --oaf-ior-fd=19
root 4408 1 0 19:17 ? 00:00:00 bt-applet --sm-disable
root 4410 1 0 19:17 ? 00:00:00 /usr/libexec/trashapplet --oaf-activate-iid=OAFIID:GNOME_Panel_TrashApplet_Factory --oaf-ior-fd=20
root 4428 1 0 19:17 ? 00:00:00 /usr/bin/python -tt /usr/bin/puplet
root 4436 1 0 19:17 ? 00:00:00 nm-applet --sm-disable
root 4439 1 0 19:17 ? 00:00:01 /usr/libexec/mixer_applet2 --oaf-activate-iid=OAFIID:GNOME_MixerApplet_Factory --oaf-ior-fd=21
root 4442 1 0 19:17 ? 00:00:00 ./escd --key_Inserted="/usr/bin/esc" --on_Signal="/usr/bin/esc"
root 4444 1 0 19:17 ? 00:00:00 /usr/libexec/clock-applet --oaf-activate-iid=OAFIID:GNOME_ClockApplet_Factory --oaf-ior-fd=23
root 4445 1 0 19:17 ? 00:00:00 /usr/lib/scim-1.0/scim-helper-manager
root 4446 1 0 19:17 ? 00:00:00 /usr/lib/scim-1.0/scim-panel-gtk --display :0.0 -c socket -d --no-stay
root 4450 1 0 19:17 ? 00:00:00 /usr/lib/scim-1.0/scim-launcher -d -c socket -e socket -f x11
root 4452 1 0 19:17 ? 00:00:00 pam-panel-icon --sm-client-id default0
root 4453 1 0 19:17 ? 00:00:00 gnome-power-manager
root 4457 1 0 19:17 ? 00:00:00 /usr/sbin/nm-system-settings --config /etc/NetworkManager/nm-system-settings.conf
root 4459 4452 0 19:17 ? 00:00:00 /sbin/pam_timestamp_check -d root
root 4464 1 0 19:17 ? 00:00:00 /usr/libexec/notification-area-applet --oaf-activate-iid=OAFIID:GNOME_NotificationAreaApplet_Factory --oaf-ior-fd=25
root 4487 1 0 19:17 ? 00:00:01 gnome-screensaver
root 24797 1 0 19:52 ? 00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/lib/mysql/localhost.localdomain.pid
mysql 24820 24797 0 19:52 ? 00:00:00 /usr/sbin/mysqld --basedir=/ --datadir=/var/lib/mysql --user=mysql --pid-file=/var/lib/mysql/localhost.localdomain.pid --skip-external-locking
命令框数据太多,可输入clear 与Dos的cls有异曲同工之处;
Wc:统计行数
[root@localhost ~]# wc -w install.log #统计单词数
1944 install.log
[root@localhost ~]# wc -m install.log #统计字符数
31657 install.log
[root@localhost ~]# wc -l install.log #统计行数
968 install.log
Expr算法命令符的运用:
[root@localhost ~]# expr 12 \* 4
48
[root@localhost ~]# expr 12 % 4
0 #求余,一般求整数
[root@localhost ~]# expr 34 / 12
2
mv文件改名使用:(主要移动文件为主)
[root@localhost home]# ls
1. zip kkk kkk.sh
[root@localhost home]# mv kkk.sh kk5.sh
[root@localhost home]# ls
1. zip kk5.sh kkk
su的用户切换:
[root@localhost home]# adduser kkk
[root@localhost home]# su kkk
[kkk@localhost home]$ su root
口令:123456
[root@localhost home]#
Passwd用户密码:
[root@localhost home]# passwd kkk
Changing password for user kkk.
New UNIX password:123
New UNIX password: 123
BAD PASSWORD: it is WAY too short
Retype new UNIX password: 123
passwd: all authentication tokens updated successfully.
[root@localhost home]# su kkk
[kkk@localhost home]$ su kkk
口令:123
[kkk@localhost home]$
类似上面操作,先touch一个.sh的文件,然后vim进行编辑使用简单#!/bin/sh编程框架,再加用echo对输出显示进行功能编辑,使其达到效果。
[root@localhost ~]#touch hh.sh
[root@localhost ~]# vim hh.sh
解压:
[root@localhost ~]# cd /home
[root@localhost home]# ls -l
总计 12
-rw-r--r-- 1 root root 2962 06-23 23:26 1.zip
-rw-r--r-- 1 root root 0 06-24 16:27 26.txt
-rw-r--r-- 1 root root 0 06-24 11:11 66.sh
-rwxr--r-- 1 root root 114 06-23 22:49 kk5.sh
drwx------ 4 kkk kkk 4096 06-24 10:57 kkk
[root@localhost home]# unzip 1.zip
Archive: 1.zip
inflating: kk1.txt
inflating: kk1。txt
[root@localhost home]# ls -l
总计 28
-rw-r--r-- 1 root root 2962 06-23 23:26 1.zip
-rw-r--r-- 1 root root 0 06-24 16:27 26.txt
-rw-r--r-- 1 root root 0 06-24 11:11 66.sh
-rw-r--r-- 1 root root 12241 06-23 22:42 kk1.txt
-rw-r--r-- 1 root root 147 06-23 22:41 kk1。txt
-rwxr--r-- 1 root root 114 06-23 22:49 kk5.sh
drwx------ 4 kkk kkk 4096 06-24 10:57 kkk
Ln链接文件:
[root@localhost home]# cd /etc/dev.d/default
[root@localhost default]# vim default.txt
[root@localhost default]# ls -l
总计 4
-rw-r--r-- 1 root root 262 06-23 23:13 default.txt
[root@localhost default]# cd
[root@localhost ~]# ln -s /etc/dev.d/default/default.txt /home
[root@localhost ~]# cd /home
[root@localhost home]# ls
default.txt kk1.txt kk1。txt kkk kkk.sh
[root@localhost home]# cd
[root@localhost ~]# cd /etc/dev.d/default
[root@localhost default]# ls
default.txt
软链接
删除:
[root@localhost default]# cd
[root@localhost ~]# rm default.txt
rm: lstat “default.txt” 失败: 没有那个文件或目录
[root@localhost ~]# rm /home/default.txt
rm:是否删除 符号链接 “/home/default.txt”? y
[root@localhost ~]# cd /home
[root@localhost home]# ls
kk1.txt kk1。txt kkk kkk.sh
zip:
[root@localhost ~]# cd /home
[root@localhost home]# ls
kk1.txt kk1。txt kkk kkk.sh
[root@localhost home]# zip 1.zip kk1.txt kk1。txt
adding: kk1.txt (deflated 79%)
adding: kk1。txt (deflated 10%)
cp –r 源文件 目标文件存放地址
[root@localhost ~]# ls
?????? 26.txt~ h install.log.syslog
123 anaconda-ks.cfg hh.sh :wq
24.doc Desktop i yy
26.txt firstboot.1434813890.05 install.log
[root@localhost ~]# cp -r 26.txt /home
[root@localhost ~]# cd /home
[root@localhost home]# ls
1.zip 26.txt 66.sh kk5.sh kkk
编辑框内:
#!/bin/sh
echo -e "efg\thello\tlllo"
:wq
[root@localhost ~]#ls –l /root/ hh.sh
-rw-r--r-- 1 root root 40 06-24 16:36 /root/hh.sh
[root@localhost ~]# chmod 744 hh.sh
[root@localhost ~]# ./hh.sh
efghellolllo
Echo:编辑特技,真的是加上的特技,没它,duang都没用!
/root/hh.sh[root@localhost ~]# echo -e "efg\thello\tlllo" (-e加空格显示)
efghellolllo
[root@localhost ~]# echo -e "efg\nhell" (-e 加\n换行显示)
efg
hell
ps:系统状态监控
[root@localhost ~]# ps -a
PID TTY TIME CMD
10639 pts/3 00:00:00 ps
显示所有用户:
[root@localhost ~]# ps -u
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.7/FAQ
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 4169 0.0 0.0 1664 428 tty1 Ss+ 13:55 0:00 /sbin/mingetty
root 4170 0.0 0.0 1664 420 tty2 Ss+ 13:55 0:00 /sbin/mingetty
root 4171 0.0 0.0 1664 424 tty3 Ss+ 13:55 0:00 /sbin/mingetty
root 4172 0.0 0.0 1664 420 tty4 Ss+ 13:55 0:00 /sbin/mingetty
root 4173 0.0 0.0 1664 424 tty5 Ss+ 13:55 0:00 /sbin/mingetty
root 4189 0.0 0.0 1664 424 tty6 Ss+ 13:55 0:00 /sbin/mingetty
root 4279 0.1 1.0 17928 10940 tty7 Ss+ 13:55 0:14 /usr/bin/Xorg :
root 5066 0.0 0.1 5648 1412 pts/1 Ss+ 13:56 0:00 bash
root 9217 0.0 0.1 5648 1412 pts/3 Ss 16:09 0:00 -bash
root 10760 0.0 0.0 5368 952 pts/3 R+ 17:22 0:00 ps –u
ps –ef或者ps —aux 显示当前系统中所有进程(看参数说话,-e 所有进程,-f完整显示进程信息)
可结合(kill -pid 号)进行关闭进程,kill 参数:-1 让进程重新读取配置文件;-9:强制关掉进程或任务; -15:停止该进程。
强制退出操作的快捷键:ctrl+z
[root@localhost ~]# vim dd.sh
[1]+ Stopped vim dd.sh
[root@localhost ~]#
[root@localhost ~]# vmstat
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 0 294100 105816 386372 0 0 20 11 1027 192 0 1 98 1 0
Tail:tail -2 文件名 查看文件尾的倒数1.2行,而head 则相反。可结合grep 和|管道命令符,tee –a(将输出内容追加到指定文件的内容末尾,不覆盖原文原有文件内容)等输出运用.
例:ps -ef |tee -a kk1.txt
Find :
[root@localhost ~]# find ~ -name "*.txt" -print
/root/.mozilla/firefox/nc2sejgk.default/signons3.txt
/root/26.txt
#在$HOME中查.txt文件并显示
[root@localhost ~]# find . -name "[A-B]*" -print
./.mozilla/firefox/nc2sejgk.default/Cache/AEFD807Fd01
./.mozilla/firefox/nc2sejgk.default/Cache/A1D23B59d01
./.mozilla/firefox/nc2sejgk.default/Cache/B89CC3BDd01
./.mozilla/firefox/nc2sejgk.default/Cache/A57F4195d01
./.mozilla/firefox/nc2sejgk.default/Cache/A18F738Ed01
./.mozilla/firefox/nc2sejgk.default/Cache/AED4F3D7d01
./.mozilla/firefox/nc2sejgk.default/Cache/A85D0B17d01
./.mozilla/firefox/nc2sejgk.default/Cache/A79FFB5Fd01
./.mozilla/firefox/nc2sejgk.default/Cache/B6E31FA3d01
./.mozilla/firefox/nc2sejgk.default/Cache/B610C565d01
./.mozilla/firefox/nc2sejgk.default/Cache/BE6E7780d01
./.mozilla/firefox/nc2sejgk.default/Cache/B248F9F3d01
./.mozilla/firefox/nc2sejgk.default/Cache/A45BAE90d01
./.mozilla/firefox/nc2sejgk.default/Cache/B79EF705d01
./.mozilla/firefox/nc2sejgk.default/Cache/B1C2B634d01
#查以大写字母开头的文件
[root@localhost ~]# find . -size +1000000c -print
./.mozilla/firefox/nc2sejgk.default/XUL.mfasl
./.mozilla/firefox/nc2sejgk.default/Cache/27979C7Fd01
./.mozilla/firefox/nc2sejgk.default/Cache/_CACHE_003_
./.mozilla/firefox/nc2sejgk.default/XPC.mfasl
#查长度大于1Mb的文件
[root@localhost ~]# find . -size 100c -print
./.cshrc
# 查长度为100c的文件
find [-H] [-L] [-P] [path...] [expression]
[root@localhost ~]# find /home -empty
/home/kkk/.mozilla/plugins
/home/kkk/.mozilla/extensions
查找大小为0的文件或空目录
[root@localhost ~]# find /home -size -20k
/home
/home/kkk.sh
/home/kk1。txt
/home/kkk
/home/kkk/.bash_logout
/home/kkk/.bashrc
/home/kkk/.bash_profile
/home/kkk/.mozilla
/home/kkk/.mozilla/plugins
/home/kkk/.mozilla/extensions
/home/kkk/.emacs
/home/kkk/.bash_history
/home/1.zip
/home/kk1.txt
在home目录下文件大小小于20k的文件
[root@localhost ~]# find /home -links +1
/home
/home/kkk
/home/kkk/.mozilla
/home/kkk/.mozilla/plugins
/home/kkk/.mozilla/extensions
查找在/home目录下连接数大于1的文件或目录
前期强制退出的编辑文件
[root@localhost ~]# vim /home hh.sh
还有 2 个文件等待编辑
" ============================================================================
" Netrw Directory Listing (netrw v125)
" /home
" Sorted by name
" Sort sequence: [\/]$,\.h$,\.c$,\.cpp$,*,\.o$,\.obj$,\.info$,\.swp$,\.bak$,\~
" Quick Help:
" ============================================================================
../ 0 2015年06月24日 星期三 10时46分35
kkk/ 0 2015年06月24日 星期三 00时16分33
1.zip 2962 2015年06月23日 星期二 23时26分58
kk1.txt 12241 2015年06月23日 星期二 22时42分28
kk1。txt 147 2015年06月23日 星期二 22时41分47
ifconfig显示和配置网卡(第一块网卡eth0常用的,第二块lo为本地环回网卡,virbr0虚拟网卡)
[root@localhost ~]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:5A:93:EE
inet addr:192.168.40.128 Bcast:192.168.40.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe5a:93ee/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:7725 errors:0 dropped:0 overruns:0 frame:0
TX packets:9273 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:597399 (583.3 KiB) TX bytes:1387251 (1.3 MiB)
Interrupt:67 Base address:0x2024
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:2270 errors:0 dropped:0 overruns:0 frame:0
TX packets:2270 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:4030141 (3.8 MiB) TX bytes:4030141 (3.8 MiB)
网卡配置:ifconfig eth0 192.168.23.135(新的ip地址)
启动和禁用:
Ifconfig eth0 up ifconfig eth0 down
Netstat:(显示网络状况,参数复杂,常用两种,netstat –lnp用于显示当前系统启用哪些端口,netstat –an 则显示网络连接状况,比如查看4444端口,netstat –an | grep 4444 结合三个功能键,netstat 管道符| grep搜索文件)
[root@localhost ~]# netstat 192.168.40.128
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
Ping:
Dir和ls:
[root@localhost home]# dir
1.zip 26.txt 66.sh hello.bash kk1.txt kk1。txt kk5.sh kkk
[root@localhost home]# ls
1. zip 26.txt 66.sh hello.bash kk1.txt kk1。txt kk5.sh kkk
[root@localhost home]# ls -l
总计 32
-rw-r--r-- 1 root root 2962 06-23 23:26 1.zip
-rw-r--r-- 1 root root 0 06-24 16:27 26.txt
-rw-r--r-- 1 root root 0 06-24 11:11 66.sh
-rwxr--r-- 1 root root 79 06-26 11:53 hello.bash
-rw-r--r-- 1 root root 12241 06-23 22:42 kk1.txt
-rw-r--r-- 1 root root 147 06-23 22:41 kk1。txt
-rwxr--r-- 1 root root 114 06-23 22:49 kk5.sh
drwx------ 4 kkk kkk 4096 06-24 10:57 kkk
shell脚本编写:
编写shell管理脚本(一)
查看当前linux系统中能够使用的shell程序的列表
[root@localhost home]# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/bin/tcsh
/bin/csh
/bin/ksh
You have new mail in /var/spool/mail/root
确认shell程序中存在的部分链接文件:
[root@localhost ~]# ls -l /bin/*sh
-rwxr-xr-x 1 root root 735004 2008-10-21 /bin/bash
lrwxrwxrwx 1 root root 4 06-20 21:57 /bin/csh -> tcsh
lrwxrwxrwx 1 root root 21 06-20 22:01 /bin/ksh -> /etc/alternatives/ksh
lrwxrwxrwx 1 root root 4 06-20 21:54 /bin/sh -> bash
-rwxr-xr-x 1 root root 346628 2009-11-05 /bin/tcsh
切换至tcsh程序的shell环境 ,并验证是否还能使用help内部指令和外部命令ls
[root@localhost ~]# ksh
# help cd
ksh: help: not found [没有那个文件或目录]
# ls -l anaconda-ks.cfg
-rw------- 1 root root 1418 06-20 22:39 anaconda-ks.cfg
# exit //返回原来的Bash环境
使用teacher用户登录系统,并将自己的登录shell更改为tcsh(这一过程,没实操)
[teacher@localhost ~]$ grep "teacher" /etc/passwd
teacher:505:505::/home/teacher:/bin/bash
[teacher@localhost ~]$ chsh
Changing shell for teacher.
Password:
New shell [/bin/bash]: /bin/tcsh
Shell changed.
[teacher@localhost ~]$ grep teacher /etc/passwd
teacher:505:505::/home/teacher:/bin/tcsh
通过cd切换路径到/etc里,用ls查看该目录的所有文件,如下:
[root@localhost ~]# cd /etc/
[root@localhost etc]# ls
a2ps.cfg hosts.deny protocols
a2ps-site.cfg hp quotagrpadmins
acpi idmapd.conf quotatab
adjtime init.d racoon
alchemist initlog.conf rc
aliases inittab rc0.d
aliases.db inputrc rc1.d
alsa iproute2 rc2.d
alternatives isdn rc3.d
anacrontab issue rc4.d
asound.state issue.net rc5.d
at.deny java rc6.d
audisp jvm rc.d
audit jvm-commmon rc.local
autofs_ldap_auth.conf jwhois.conf rc.sysinit
auto.master kdump.conf readahead.d
auto.misc krb5.conf reader.conf
auto.net ldap.conf reader.conf.d
auto.smb ld.so.cache redhat-lsb
avahi ld.so.conf redhat-release
bashrc ld.so.conf.d resolv.conf
blkid lftp.conf resolv.conf.predhclient
bluetooth libaudit.conf rhgb
bonobo-activation libuser.conf rmt
capi.conf localtime rpc
cdrecord.conf login.defs rpm
conman.conf logrotate.conf rwtab
cron.d logrotate.d rwtab.d
cron.daily logwatch samba
cron.deny lsb-release.d sane.d
cron.hourly ltrace.conf sasl2
cron.monthly lvm scim
crontab mail scrollkeeper.conf
cron.weekly mailcap scsi_id.config
csh.cshrc mail.rc securetty
csh.login makedev.d security
cups man.config selinux
dbus-1 maven services
default mgetty+sendfax sestatus.conf
depmod.d mime.types setroubleshoot
desktop-profiles minicom.users setuptool.d
dev.d mke2fs.conf sgml
dhcp6c.conf modprobe.conf shadow
DIR_COLORS modprobe.conf~ shadow-
DIR_COLORS.xterm modprobe.d shells
dnsmasq.conf motd skel
dnsmasq.d mtab slrn.rc
dumpdates mtools.conf smartd.conf
enscript.cfg multipath.conf smrsh
environment Muttrc sound
esd.conf Muttrc.local ssh
exports netplug stunnel
fb.modes netplug.d subversion
filesystems NetworkManager sudoers
firmware nscd.conf sysconfig
fonts nsswitch.conf sysctl.conf
foomatic ntp syslog.conf
fstab ntp.conf termcap
gconf openldap tpvmlp.conf
gcrypt opt udev
gdm pam.d updatedb.conf
ghostscript pam_pkcs11 vimrc
gnome-vfs-2.0 pam_smb.conf virc
gnome-vfs-mime-magic pango vmware-tools
gpm-root.conf passwd warnquota.conf
gre.d passwd- wgetrc
group pcmcia wpa_supplicant
group- pinforc wvdial.conf
grub.conf pki X11
gshadow pm xdg
gshadow- ppp xinetd.conf
gssapi_mech.conf prelink.cache xinetd.d
gtk-2.0 prelink.conf xml
hal prelink.conf.d yp.conf
host.conf printcap yum
hosts profile yum.conf
hosts.allow profile.d yum.repos.d
由root用户使用usermod命令将teacher用户的登录shell重新修改为“/bin/bash”(此过程没实操)
[root@localhost ~]# usermod -s /bin/bash teacher
[root@localhost ~]# grep "teacher" /etc/passwd
teacher:505:505::/home/teacher:/bin/bash
使用“!wh”调用历史命令中最近一次以“wh”开头的命令并执行
[root@localhost ~]# which fdisk
/sbin/fdisk
[root@localhost ~]# pwd
/root
[root@localhost ~]# !wh
which fdisk
/sbin/fdisk
实操:
[root@localhost ~]# !wh
-bash: !wh: command not found
[root@localhost ~]# which fdisk
/sbin/fdisk
[root@localhost ~]# pwd
/root
[root@localhost ~]# !wh
-bash: !wh: command not found (不被找到)
查看用户最后执行的三条历史命令记录
[root@localhost ~]# history | tail -3 (tail –n 文件名 制定显示文件的尾多少行的内容,head则相反)
497 pwd
498 !wh
499 history | tail -3
通过“!序号”的形式执行用户历史命令列表中的第497条命令
[root@localhost ~]# !497
pwd
/root
将所有用户记录历史命令的条数设置为200
[root@localhost ~]# vi /etc/profile
# /etc/profile
# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc
pathmunge () {
if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
fi
}
# ksh workaround
if [ -z "$EUID" -a -x /usr/bin/id ]; then
EUID=`id -u`
UID=`id -ru`
fi
# Path manipulation
if [ "$EUID" = "0" ]; then
pathmunge /sbin
pathmunge /usr/sbin
pathmunge /usr/local/sbin
fi
# No core files by default
ulimit -S -c 0 > /dev/null 2>&1
if [ -x /usr/bin/id ]; then
USER="`id -un`"
LOGNAME=$USER
MAIL="/var/spool/mail/$USER"
fi
HOSTNAME=`/bin/hostname`
HISTSIZE=200
if [ -z "$INPUTRC" -a ! -f "$HOME/.inputrc" ]; then
INPUTRC=/etc/inputrc
fi
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC
for i in /etc/profile.d/*.sh ; do
if [ -r "$i" ]; then
if [ "$PS1" ]; then
. $i
else
. $i >/dev/null 2>&1
fi
fi
done
将用户jerry的历史命令记录的条数设置为5,重新选择后确认效果:
[jerry@localhost ~]$ vi ~/.bash_profile
export HISTSIZE=5
[jerry@localhost ~]$ history | wc –l
5 没实操!
[root@localhost home]# su 666
bash-3.2$
bash-3.2$
bash-3.2$
bash-3.2$ dddsa
bash: dddsa: command not found
bash-3.2$ daw
bash: daw: command not found 出现了操作失误!
使用56用户登录,执行“history –c” 命令已清空自己的命令历史记录
[root@localhost home]# adduser 56
[root@localhost home]# ls -l
总计 44
-rw-r--r-- 1 root root 2962 06-23 23:26 1.zip
-rw-r--r-- 1 root root 0 06-24 16:27 26.txt
drwx------ 3 56 56 4096 06-26 15:32 56
drwxr-xr-x 2 root root 4096 06-26 15:18 666
-rw-r--r-- 1 root root 0 06-24 11:11 66.sh
-rwxr--r-- 1 root root 90 06-26 13:58 hello.bash
-rwxr--r-- 1 root root 63 06-26 14:02 he.sh
-rw-r--r-- 1 root root 12241 06-23 22:42 kk1.txt
-rw-r--r-- 1 root root 147 06-23 22:41 kk1。txt
-rwxr--r-- 1 root root 114 06-26 13:58 kk5.sh
drwx------ 4 kkk kkk 4096 06-24 10:57 kkk
[root@localhost home]# su 56
[root@localhost home]# su 56
[56@localhost home]$ history | tail -2
1 history | tail -2
[56@localhost home]$ history -c
[56@localhost home]$ history
1 history
[56@localhost home]$
查看已设置的所有命令别名:
[root@localhost 56]# alias
alias cp='cp -i'
alias l.='ls -d .* --color=tty'
alias ll='ls -l --color=tty'
alias ls='ls --color=tty'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
使用预设的别名“ll(这里是字母ll)”实现“ls -l”命令的功能
[root@localhost ~]# ll install.log
-rw-r--r-- 1 root root 35525 06-20 22:38 install.log
[root@localhost ~]# ls -l install.log
-rw-r--r-- 1 root root 35525 06-20 22:38 install.log
设置一个命令别名dir,以便执行“ls –ld –color=tty”的命令,并进行确认
[root@localhost ~]# alias dir='/bin/ls -ld --color=tty'
[root@localhost ~]# alias dir
alias dir='/bin/ls -ld --color=tty'
[root@localhost ~]# dir install.log
-rw-r--r-- 1 root root 35525 06-20 22:38 install.log
取消上一步设置的dir命令别名
[root@localhost ~]# unalias dir
[root@localhost ~]# alias dir
bash: alias: dir: not found
取消用户当前环境中已设置的所有命令别名。
[root@localhost ~]# unalias -a
[root@localhost ~]# alias //将查不到任何别名信息
查看teacher用户的身份信息,并将输出结果保存到文件“idtch.txt”中。
[root@localhost ~]# id teacher > idtch.txt
[root@localhost ~]# cat idtch.txt
uid=505(teacher) gid=505(teacher) groups=505(teacher)
查看jerry用户的身份信息,并追加到文件“idtch.txt”中,不覆盖文件的原有内容。
[root@localhost ~]# id jerry >> idtch.txt
[root@localhost ~]# cat idtch.txt
uid=505(teacher) gid=505(teacher) groups=505(teacher)
uid=507(jerry) gid=507(jerry) groups=507(jerry)
在文件“pw.txt”中保存初始密码“123456”,并使用该文件为jerry用户设置密码。
[root@localhost ~]# vi pw.txt
123456
[root@localhost ~]# passwd --stdin jerry < pw.txt
Changing password for user jerry.
passwd: all authentication tokens updated successfully.
尝试使用help命令查看ls命令的帮助信息时,由于ls并非内部命令,系统将会报错,通过“2>”操作符可以将报错信息重定向到指定的文件中。
[root@localhost ~]# help ls 2> err.log
[root@localhost ~]# cat err.log
-bash: help: no help topics match `ls'. Try `help help' or `man -k ls' or `info ls'.
1. 使用ls命令查看“/etc/password”文件(并不存在)和“/etc/passwd”文件的属性时,输出结果将同时包含标准输出和错误输出。测试进行分别重定向、合并重定向的结果
[root@localhost ~]# ls -l /etc/password /etc/passwd > std.txt 2> err.log
[root@localhost ~]# cat std.txt
-rw-r--r-- 1 root root 2135 10-17 23:21 /etc/passwd
[root@localhost ~]# cat err.log
ls: /etc/password: 没有那个文件或目录
[root@localhost ~]# ls -l /etc/password /etc/passwd &> out.log
[root@localhost ~]# cat out.log
ls: /etc/password: 没有那个文件或目录
-rw-r--r-- 1 root root 2135 10-17 23:21 /etc/passwd
2. 过滤出“/etc/inittab”文件中不以“#”号开头的行,并再次过滤出非空行,将结果重定向保存为“inittab.txt”文件。
[root@localhost ~]# grep -v "^#" /etc/inittab | grep -v "^$" > inittab.txt
[root@localhost ~]# cat inittab.txt
找出系统中使用Bash作为登录shell、名称以“ha”开头的用户账号,并只显示出用户名、登录shell两个字段的内容
[root@localhost ~]# grep "bash"/etc/passwd | grep "^ha" | cut -d ":" -f 1,7
handy:/bin/bash
hackli:/bin/bash
查看当前系统的总内存空间、剩余可用内存空间(MB),去除其他无关信息
[root@localhost ~]# free -m | head -2
total used free shared buffers cached
Mem: 1010 577 433 0 57 366
[root@localhost ~]# free -m | grep "Mem" | awk '{print $2,$4}'
1010 433
新建立一个名为“DAY”的变量,初始内容设置为“sunday”。
[root@localhost ~]# DAY=Sunday
查看变量DAY的内容,比较使用“$”符号与不使用“$”符号的区别。
[root@localhost ~]# echo DAY
DAY
[root@localhost ~]# echo $DAY
Sunday
3.在变量DAY的内容后紧跟“MorningBus”字符串并在一起显示。
[root@localhost ~]# echo $DAYMorningBus //错误引用
[root@localhost ~]# echo ${DAY}MorningBus
sundayMorningBus
4.确认变量DAY的内容,并使用双引号为TODAY变量赋值‘Today is $DAY’。
[root@localhost ~]# echo $DAY
sunday
[root@localhost ~]# TODAY="Today is $DAY"
[root@localhost ~]# echo $TODAY
Today is Sunday
5. 确认变量DAY的内容,并使用单引号为TODAY变量赋值‘Today is $DAY’。
[root@localhost ~]# echo $DAY
sunday
[root@localhost ~]# TODAY='Today is $DAY'
[root@localhost ~]# echo $TODAY
Today is $DAY
6.统计当前登录到本地终端(tty1~tty6)中的用户数量,并将结果保存到变量UserNum中。
[root@localhost ~]# UserNum=`w | grep "tty" | wc -1`
[root@localhost ~]#echo $UserNum
用一行命令找出安装了fdisk 程序的软件包名称(需要先确定fdisk 程序的
文件位置)。
[root@localhost ~]# rpm -qf `which fdisk`
util-linux-2.13-0.52.el5_4.1
通过find 命令找出系统中hackli 用户留下的文件或目录,并使用rm 命令
将其删除。
[root@localhost ~]# rm -rf `find /home -user hackli`
从键盘输入一整行数据,赋值给变量HELO,并确认HELO 变量的内容。
[root@localhost ~]# read HELO
good morning ,teacher!
[root@localhost ~]# echo $HELO
good morning ,teacher!
从键盘输入一整行数据,依次赋值给变量G1、G2,并确认G1、G2 变量
的内容。
[root@localhost ~]# read G1 G2
good morning,teacher!
[root@localhost ~]# echo $G1
good
[root@localhost ~]# echo $G2
morning,teacher!
从键盘读入一个数值变量,并给出相应的提示信息。
[root@localhost ~]# read -p "please input a number:" YourNum
please input a number:501
[root@localhost ~]# echo $YourNum
501
在当前shell环境中定义一个变量FILESVR,开启一个新的子shell进程(Bash
或Zsh 等),处于子shell 环境中时将无法使用变量FILESVR 的内容。
[root@localhost ~]# FILESVR=filesvr.benet.com
[root@localhost ~]# zsh
[root@localhost ~]# echo $FILESVR
[root@localhost ~]# exit
[root@localhost ~]# echo $FILESVR
filesvr.benet.com
确认变量FILESVR 的内容,并将其设置为全局变量,在子shell、当前shell
环境中进行验证。()
[root@localhost ~]# echo $FILESVR
filesvr.benet.com
[root@localhost ~]# export FILESVR
[root@localhost ~]# zsh
[root@localhost ~]# echo $FILESVR //在子shell 环境中也可以使用该变量
filesvr.benet.com
[root@localhost ~]# exit
[root@localhost ~]# echo $FILESVR //返回原shell 环境中仍然能使用该变量
filesvr.benet.com
定义两个变量MONTH、YEAR,并将其设置为全局变量
[root@localhost ~]# export MONTH=May YEAR=2015
[root@localhost ~]# echo $MONTH$YEAR
May2015
清除已设置的变量DAY、MONTH、YEAR
[root@localhost ~]# unset DAY MONTH YEAR
[root@localhost ~]# ECHO $DAY $MONTH $YEAR
-bash: ECHO: command not found
设置变量X、Y 的值分别为34、12,依次计算变量X、Y 的加减乘除及取
模运算结果
[root@localhost ~]# X=34
[root@localhost ~]# Y=12
[root@localhost ~]# expr $X + $Y
46
[root@localhost ~]# expr $X - $Y
22
[root@localhost ~]# expr $X \* $Y
408
[root@localhost ~]# expr $X / $Y
2
[root@localhost ~]# expr $X % $Y
10
分页查看系统环境中设置的shell 变量:
[root@localhost home]# set | less
BASH=/bin/bash
BASH_ARGC=()
BASH_ARGV=()
BASH_LINENO=()
BASH_SOURCE=()
………
IFS=$' \t\n'
以分号分隔,显示当前用户的用户名、宿主目录、登录shell。
[root@localhost home]# echo "$USER;$HOME;$SHEL"
56;/root;
确认当前的命令搜索路径,并将“/opt/bin”目录添加到现有的搜索路径
中去
[root@localhost home]# echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost home]# PATH="/opt/bin;$PATH"
[root@localhost home]# echo $PATH
/opt/bin;/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
在当前用户环境中,将用于限制历史命令条数的环境变量HISTSIZE 的值改为26
[root@localhost home]# history |wc -l
200
[root@localhost home]# export HISTSIZE=26
[root@localhost home]# echo $HISTSIZE
26
[root@localhost home]# history |wc -l
26
编辑“~/.bash_profile”文件,修改PATH 的设置,以便用户在下次登录
后能够使用“/opt/bin”目录作为默认搜索路径
[root@localhost ~]# vi ~/.bash_profile
PATH=$PATH:$HOME/bin:/opt/bin
Export PATH
验证预定义变量$0、$$、$?的作用。
[root@localhost ~]# bash //开启一个新的进程
[root@localhost ~]# echo $0 $$ //显示当前的进程名称、PID 号
bash 2040
[root@localhost ~]# exitt //执行一个错误命令
bash: exxit: command not found
[root@localhost ~]# echo $? //返回值非0,表示上一条命令执行失败
127
[root@localhost ~]# exit
exit
[root@localhost ~]# echo $? //返回值为0,表示上一条命令执行成功
0
检查“/var/log/messages”文件是否存在,若存在则统计文件内容的行数
并输出,否则不做任何操作(合理使用变量,可以提高编写效率)
[root@localhost ~]# vim chklog.sh
[root@localhost ~]# sh chklog.sh
chklog.sh: line 3: syntax error near unexpected token `then'
chklog.sh: line 3: `if[-f $LogFile]; then'
提示用户指定备份目录的路径,若目录已存在则显示提示信息后跳过,否
则显示相应提示信息后创建该目录
[root@localhost ~]# vim mkbak.sh
#!/bin/bash
read -p " What is your backup directory: " BakDir
if[-d $BakDir]; then
echo "$BakDir already exist,'
`else`
echo "$BakDir is not exist,will make it."
mkdir $BakDir
`fi`
[root@localhost ~]# sh mkbak.sh
What is your backup directory:/opt/bakroot
mkbak.sh: line 3: syntax error near unexpected token `then'
mkbak.sh: line 3: `if[-d $BakDir]; then'
[root@localhost ~]# ls -ld /opt/bakroot/
ls: /opt/bakroot/: No such file or directo
.ry
统计当前登录到系统中的用户数量,并判断是否超过三个,若是则显示实
际数量并给出警告信息,否则列出登录的用户账号名称及所在终端
[root@localhost ~]# vim chkuser.sh
#!/bin/bash
if [ $UserNum -gt 3 ] ; then
echo "Alert, too many login users ( Total: $UserNum ). "
else
echo "Login users: "
who | awk '{print $1,$2}'
fi
[root@localhost ~]# sh chkuser.sh
chkuser.sh: line 2: [: -gt: unary operator expected
Login users:
root pts/1
root :0
检查 portmap进程是否已经存在,若已经存在则输出“portmap service if
running.” ;否则检查是否存在“/etc/rc.d/init.d/portmap” 可执行脚本,存在
则启动 portmap服务,否则提示“no portmap script file.”
[root@localhost ~]# vim chkportmap.sh
#!/bin/bash
pgrep portmap & > /dev/null
if [ $? -eq 0 ] ; then
echo " portmap service is running ."
elif [ -x " /etc/rc.d/init.d/portmap " ] ; then
service portmap start
else
echo " no portmap script file ."
fi
[root@localhost ~]# sh chkportmap.sh
portmap service is running .
[root@localhost ~]# 3593
[root@localhost ~]# vim chkportmap.sh
#!/bin/bash
service mysqld status $ > /dev/null
if [ $? -ne 0 ] ; then
echo "At time: `date`: MySQL server is down. " >> /var/log/messages
rvice mysqld restart
fi
[root@localhost ~]# tail -1 /var/log/messages #读出路径/var/log/messages目录下的倒数一行的信息
[root@localhost ~]# crontab –e
*/5 * * * * /root/chkdbsvr.sh #需确认crond服务已运行
依次输出三条文字信息,包括一天中的“Moring”、“Noon”、“Evening”
字串
[root@localhost ~]# vim showday.sh
#!/bin/bash
for TM in “ Morning “ “Noon” “Evening” #for TM in 文字信息输入
do #执行如下语句
echo “" the $TM of the day " #of 后面可以随意编写名字
done #完成
[root@localhost ~]# sh showday.sh
the Morning of the day .
the Noon of the day .
the Evening of the day .
[root@localhost ~]# vim chkfileown.sh
#!/bin/bash
DIR=" /opt "
LMT=100
ValidUser=`grep " /bin/bash " /etc/passwd | cut -d ":" -f 1 `
for UserName in $ValidUsers
do
Num=` grep " /bin/bash "/etc/passwd |wc -l`
echo " $UserName have $Num files. "
`fi`
Done
[root@localhost ~]# sh chkfileown.sh
[root@localhost ~]# find -user root | wc -l
120
[root@localhost ~]# vim confsize.sh
#!/bin/bash
SizeNums=$(ls -l $(find /etc -type f -a -name *.conf ) | awk '{ print $5 } ' ) # /etc文件目录可以随意
Total= 0
for i in $SizeNums
do
Total=`expr $Total + $i `
done
echo " Total size of conf files: $Total bytes."
[root@localhost ~]# sh confsize.sh
confsize.sh: line 3: 0: command not found
Total size of conf files: 467459 bytes.
创建函数:(shell 高级运用创建函数)
Vim myfun.sh
#!/bin/bash
# testing the script
function myfun {
echo "This is an example of a function"
}
count=1
while [ $count -le 5 ]
do
myfun
count=$[ $count +1 ]
done
echo "This is the end of the loop"
myfun
#chmod 744 myfun.sh
#./myfun.sh
This is an example of a function
This is an example of a function
This is an example of a function
This is an example of a function
This is an example of a function
This is the end of the loop
This is an example of a function
使用return命令:
[root@localhost ~]# vim 52.sh
#!/bin/bash
# testing the script
function myfun {
read -p "Enter a value:" value
echo $[ $value * 2 ]
}
result=`myfun`
echo "The new vlue is $result"
#chmod 744 52.sh
#./52.sh
[root@localhost ~]# ./52.sh
Enter a value:23
The new vlue is 46
结果:
(退出状态的取值范围是0到255,$?是最近已执行命令的退出状态)
例子:
[root@localhost ~]# vim 6.sh
#!/bin/bash
# testing the script
function myfun {
read -p "Enter a value:" value
echo $[ $value * 2 ]
}
result=`myfun`
echo "The new vlue is $result"
[root@localhost ~]# ./6.sh
Enter a value:1
The new vlue is 2
向函数传递参数
#!/bin/bash
# testing the script
function addem {
if [ $# -eq 0 ]||[ $# -gt 2 ]
echo -n "More than two numbers:" then
echo -1
elif [ $# -eq 1 ]
then
echo $[ $1 + $1 ]
else
echo $[ $1 + $2 ]
fi
}
echo -n "Adding 10 and 15:"
value=`addem 10 15`
echo $value
echo -n "Just one number 10:"
value=`addem 10`
echo $value
echo -n "No numbers:"
value=`addem`
echo $value
vaule=`addem 10 15 20`
echo $value
结果:
Adding 10 and 15:25
Just one number 10:20
No numbers:-1
(由上述可知,#可以得到参数的个数, 1表示第一个参数,$2表示第二个参数)
#!/bin/bash
#testing the script
function addem {
if [ $# -eq 0 ] || [ $# -gt 2 ]
then
echo -1
elif [ $# -eq 1 ]
then
echo $[ $1 + $1 ]
else
echo $[ $1 + $2]
fi
}
if [ $# -eq 2 ]
then
value=`addem $1 $2`
echo " The value is $value "
else
echo " Usage: test1 a b "
fi
[root@localhost ~]# ./function.sh
Usage: test1 a b
(函数外面的#表示调用脚本使用的参数数,函数里的#表示调用函数使用的参数数,注意这点区别)作用域是变量的可见区域。
全局变量,在shell脚本内处处有效的变量。函数中定义全局变量,那么主代码中有效。主代码中定义全局,函数中也有效。
默认情况下,脚本中定义的变量都是全局变量
#!/bin/bash
# testing the script
function myfun {
value=$[ $value * 2 ]
}
read -p "Enter a value:" value
myfun
echo "The new value is:$value"
[root@localhost ~]# ./function.sh
Enter a value:3
The new value is:6
输入3,输出6,这里的value在函数中发生变化了,到脚本中一样可以使用,而且已经变化了
#!/bin/bash
# testing the script
function myfun {
local value=$[ $value * 2 ]
}
read -p "Enter a value:" value
myfun
echo "The new value is:$value"
(输入45,输出45。因为加上local关键字之后,函数中的value是局部变量,与外界无关)
#!/bin/bash
# testing the script
function addarray {
local sum=0
local newarray
newarray=(`echo "$@"`)
for value in ${newarray[*]}
do
sum=$[ $sum + $value ]
done
echo ${newarray[*]}
}
myarray=(1 2 3 4 5)
echo "The original array is :${myarray[*]}"
arg=`echo ${myarray[*]}`
result=`addarray $arg`
echo "The result is $result"
结果:
The original array is :1 2 3 4 5
The result is 15
(数组参数传入函数,函数获取后,进行处理输出。脚本将输出结果打印)
shell脚本实现菜单操作
#!/bin/bash
#written by wubo
#blog:blog.csdn.net/wbls615117
while :
do
echo "请输入你要进行的操作:"
select var in "edit file" "view ip" "delete file" "change directory" "exit" "view directory"
do
break
done
case $var in
"edit file")
echo -n "please input edit file:"
read file
vim $file
echo '编辑文件成功'
;;
"view ip")
echo -n "please input device name:"
read file
ifconfig $file
echo '显示IP地址成功'
;;
"delete file")
echo -n "please input delete file:"
read file
rm -rf $file
echo '成功删除文件'
;;
"change directory")
echo -n "please input change directory:"
read file
cd $file
echo "当前目录为:$(pwd)"
;;
"view directory")
echo -n "please input a directory:"
read file
ls $file
echo "目录浏览成功"
;;
"exit")
break
echo '退出成功'
;;
*)
break
echo '退出成功'
;;
esac
done
Shell的while:
[root@localhost ~]# vim while1.sh
#!/bin/bash
a=1
while (( $a<= 20 ))
do
echo $a
let "a++"
done
[root@localhost ~]#chmod 744 while1.sh
[root@localhost ~]#./while1.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/bin/bash
a=1
while (( $a<= 20 ))
do
echo $a
let "a++"
done
[root@localhost ~]# ./while.sh
10
9
8
7
6
用脚本使用结束标记控制while循环实现猜出1-100内的数:
[root@localhost ~]# vim while3.sh
#!/bin/sh
echo "Please input the num (1~~100): "
read num
while [[ $num != 50 ]]
do
if [ $num -lt 50 ]
then
echo "Too small ,Try again.."
read num
elif [ $num -gt 50 ]
then
echo "Too big ,Try again.. "
read num
else
exit 0
fi
done
echo "Yes ,you are right !!"
[root@localhost ~]#chmod 744 while3.sh
[root@localhost ~]#ls -l while3.sh
-rwxr--r-- 1 root root 263 07-01 22:30 while3.sh
[root@localhost ~]# ./while3.sh
Please input the num (1~~100):
52
Too big ,Try again..
47
Too small ,Try again..
50
Yes ,you are right !!
[root@localhost ~]# vim while4.sh
#!/bin/sh
echo "Please input the num:"
read num
sum=0
i=1
signal=0
while [[ $signal != 1 ]]
do
if [ $i -eq $num ]
then
let "signal=1"
let "sum+=i"
echo "1+2、、、+$num=$sum"
else
let "sum=sum+i"
let "i++"
fi
done
[root@localhost ~]# ./while4.sh
Please input the num:
5
1+2、、、+5=15
[root@localhost ~]# ./while4.sh
Please input the num:
255
1+2、、、+255=32640
类似这种程序不涉及linux系统函数,所以与VC++6.0下编一样main()
{
int n=1;
int sum=0;
while(n<=100)
{sum+=n;
n++;
}
}
命令行控制的while循环
[root@localhost ~]# vim while5.sh
#!/bin/sh
echo "Please input arguement is $# "
echo "What you input:"
while [[ $* != "" ]]
do
echo $1
shift
done
[root@localhost ~]#chmod 744 while5.sh
[root@localhost ~]# ./while5.sh
Please input arguement is 0
What you input:
以后会不断地更新此文章,敬请关注!