1
2
3
4
5
6
7
8
|
#!/bin/bash
# Program:
# Showing who you are and where you are.
# History:
#
2015
/
07
/
26
XPleaf First release
PATH=$PATH:~/bin
export PATH
echo
"the user is '$(whoami)',and you are in '$(pwd)'"
|
1
2
|
[root@moban test]# sh sh01.sh
the user
is
'root'
,and you are
in
'/root/scripts/test'
|
-
用户输入类似19960621格式的生日
-
将用户的生日转换为当年的(比如今年是2015):20159621
-
将当前日期和用户生日日期(已经转化为当年)都转换为用秒来表示
-
距离生日秒数=用户生日日期秒数-当前日期秒数
-
将“距离生日秒数”转换为天数
-
判断该天数的正负,从而得知用户的生日过了没有
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#!/bin/bash
# Program:
# Telling you how many days are there before your birthday.
# History:
#
2015
/
07
/
26
XPleaf First release
PATH=$PATH:~/bin
export PATH
read -p
"Please input your birthday like <19960621> : "
bith_input
bith_input_MD=$(echo $bith_input | awk
'BEGIN {FS=""} {print $5$6$7$8}'
)
declare -i now_s=`date +%s`
declare -i bith_s=`date --date=
"$(date +%Y)$bith_input_MD"
+%s`
declare -i total_s=$(($bith_s-$now_s))
declare -i total_day=$(($total_s/
60
/
60
/
24
))
if
[
"$total_day"
-gt
"0"
];then
echo
"There are $total_day day(s) before your bithday."
elif [
"$total_day"
-eq
"0"
];then
echo
"Today is your birthday!!!~Happy Birthday to you!"
else
echo
"Your birthday in this year had gone!"
fi
|
1
2
3
|
[root@moban test]# sh sh02.sh
Please input your birthday like <
19960621
> :
20150730
There are
3
day(s) before your bithday.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#!/bin/bash
# Program:
# You input the number, and I caculate
1
+
2
+
3
+...+the number.
# History:
#
2015
/
07
/
26
PATH=$PATH:~/bin
export PATH
echo
"You input the number, and I caculate 1+2+3+...+the number."
read -p
"Please input the number: "
nu
for
((i=s=
0
;i<=$nu;i++))
do
s=$(($s+$i))
done
echo
"1+2+3+...+$nu ==> $s"
|
1
2
3
4
|
[root@moban test]# sh sh03.sh
You input the number, and I caculate
1
+
2
+
3
+...+the number.
Please input the number:
100
1
+
2
+
3
+...+
100
==>
5050
|
-
判断/root/test目录下的logical这个名称是否存在
(1)不存在 ==>使用touch来创建logical这个文件
(2)存在
a.logical是文件 ==>删除logical文件,同时创建logical目录
b.logical是目录 ==>删除logical目录
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#!/bin/bash
# Program:
# check the /root/test/logical
# History:
#
2015
/
07
/
26
XPleaf First release
PATH=$PATH:~/bin
export PATH
echo
"check the /root/test/logical"
test -e /root/test/logical
i=$?
if
[
"$i"
-eq
"0"
];then
test -f /root/test/logical && rm -rf /root/test/logical && mkdir /root/test/logical || rm -rf /root/test/logical
else
touch /root/test/logical
fi
|
1
2
|
[root@moban test]# ls -l /root/test/logical
ls: 无法访问/root/test/logical: 没有那个文件或目录
|
1
2
3
4
|
[root@moban test]# sh sh04.sh
check the /root/test/logical
[root@moban test]# ls -l /root/test/logical
-rw-r--r--
1
root root
0
7
月
26
11
:
05
/root/test/logical
|
1
2
3
4
|
[root@moban test]# sh sh04.sh
check the /root/test/logical
[root@moban test]# ls -ld /root/test/logical
drwxr-xr-x
2
root root
4096
7
月
26
11
:
06
/root/test/logical
|
1
2
3
4
|
[root@moban test]# sh sh04.sh
check the /root/test/logical
[root@moban test]# ls -l /root/test/logical
ls: 无法访问/root/test/logical: 没有那个文件或目录
|
1
2
3
4
5
6
7
8
9
10
|
#!/bin/bash
# Program:
# Remove the first list of the passwd and then output it
# like "The count line one
is
: the content of the passwd.
# History:
#
2015
/
07
/
26
XPleaf First release
PATH=$PATH:~/bain
export PATH
echo
"$(awk -F "
:
" '{print "
The
" NR "
account
is
" $1}' /etc/passwd)"
|
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
|
[root@moban test]# sh sh05.sh
The
1
account
is
root
The
2
account
is
bin
The
3
account
is
daemon
The
4
account
is
adm
The
5
account
is
lp
The
6
account
is
sync
The
7
account
is
shutdown
The
8
account
is
halt
The
9
account
is
mail
The
10
account
is
uucp
The
11
account
is
operator
The
12
account
is
games
The
13
account
is
gopher
The
14
account
is
ftp
The
15
account
is
nobody
The
16
account
is
dbus
The
17
account
is
vcsa
The
18
account
is
abrt
The
19
account
is
haldaemon
The
20
account
is
ntp
The
21
account
is
saslauth
The
22
account
is
postfix
The
23
account
is
sshd
The
24
account
is
tcpdump
The
25
account
is
oldboy
The
26
account
is
test
The
27
account
is
apache
The
28
account
is
www
The
29
account
is
mysql
|
1
|
user=$(sed -n
'$ip'
/etc/passwd | cut -d
':'
-f
1
)
|