[root@localhost ~]# NAME=Beny
[root@localhost ~]# echo $NAME
Beny
[root@localhost ~]# bash
[root@localhost ~]# echo $NAME
引用变量:${VARNAME} ,括号有时可省略
[root@localhost ~]# ANIMAL=pig
[root@localhost ~]# echo "There are some $ANIMALs.
> "
There are some .
[root@localhost ~]# echo "There are some ${ANIMAL}s."
There are some pigs.
单引号是强引用,不实现变量替换。双引号是弱引用,可以实现变量替换。
[root@localhost ~]# echo 'There are some ${ANIMAL}s.'
There are some ${ANIMAL}s.
[root@localhost ~]# export NAME
[root@localhost ~]# echo $NAME
Beny
[root@localhost ~]# bash
[root@localhost ~]# echo $NAME
Beny
[root@localhost ~]# bash
[root@localhost ~]# echo $NAME
Beny
[root@localhost shell_example]# bash -x filetest1.sh /etc/fstab
+ '[' '!' -e /etc/fstab ']'
+ echo OK
OK
[root@localhost shell_example]# bash -x filetest1.sh
+ '[' '!' -e ']'
+ echo OK
OK
#!/bin/bash
echo $#
echo $*
echo $1
shift
echo $1
shift
echo $1
[root@localhost shell_example]# bash -x shift.sh 1 2 3
+ echo 3
3
+ echo 1 2 3
1 2 3
+ echo 1
1
+ shift
+ echo 2
2
+ shift
+ echo 3
3
#!/bin/bash
if [ $# -lt 2 ] ; then
echo "usage: ./add.sh arg1 arg2"
exit 3
fi
echo "The sum is : $[$1+$2]."
echo "The prod is : $[$1*$2]."
[root@localhost shell_example]# ./add.sh 2 6
The sum is : 8.
The prod is : 12.
[root@localhost shell_example]# ./add.sh 2 6 3
The sum is : 8.
The prod is : 12.
[root@localhost ~]# ls /var/
account caozesheng_py empty gopher local mail preserve target yp
adm crash games kerberos lock nis run tmp
cache db gdm lib log opt spool var
[root@localhost ~]# echo $?
0
[root@localhost ~]# ls /vaarr
ls: 无法访问/vaarr: 没有那个文件或目录
[root@localhost ~]# echo $?
2
[root@localhost ~]# lss /var
bash: lss: 未找到命令...
相似命令是: 'ls'
[root@localhost ~]# echo $?
127
$#:参数的个数
$* :参数列表
$@:参数列表
#!/bin/bash
echo $#
echo $*
echo $@
if [ $# -lt 1 ] ;then
echo "Usage: ./filetest1.sh arg1 [arg2,....]"
exit 7
fi
if [ ! -e $1 ] ; then
echo "no such file"
else
echo "OK"
fi
[root@localhost ~]# echo $NAME
Beny
[root@localhost ~]# unset NAME
[root@localhost ~]# echo $NAME
[root@localhost ~]# ANIMALS=pig
[root@localhost ~]# ANIMALS=$ANIMALS:goat
[root@localhost ~]# echo $ANIMALS
pig:goat
[root@localhost ~]# ANIMALS=$ANIMALS:sheep
[root@localhost ~]# echo $ANIMALS
pig:goat:sheep
[root@localhost ~]# a=1
[root@localhost ~]# b=2
[root@localhost ~]# c=$a+$b
[root@localhost ~]# echo $c
1+2
[root@localhost ~]# bash first.sh
#
# /etc/fstab
# Created by anaconda on Sat Apr 16 18:39:54 2016
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=0c8c0c2c-d3b8-4658-b899-0fed6a2e94d2 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
account caozesheng_py empty gopher local mail preserve target yp
adm crash games kerberos lock nis run tmp
cache db gdm lib log opt spool var
#!/usr/bin/bash
for i in `seq 5`
do
useradd user${i}
echo user${i} | passwd --stdin user${i} &> /dev/null
echo "用户 user${i} 已经成功添加"
done
#!/usr/bin/bash
USER=user1
userdel -r $USER
echo "用户删除完成"
[root@localhost shell_example]# A=3
[root@localhost shell_example]# B=6
[root@localhost shell_example]# [ $A -eq $B ]
[root@localhost shell_example]# echo $?
1
[root@localhost shell_example]# B=3
[root@localhost shell_example]# [ $A -eq $B ]
[root@localhost shell_example]# echo $?
0
[root@localhost shell_example]# id user2 &> /dev/null && echo "Hello, user2"
Hello, user2
[root@localhost shell_example]# id user1 &> /dev/null && echo "Hello, user2"
#!/bin/bash
echo $1
case $1 in
[0-9])
echo "A digit" ;;
[a-z])
echo "Lower" ;;
[A-Z])
echo "Upper" ;;
*)
echo "Spicial character" ;;
esac
#!/bin/bash
grep "\ /dev/null
RETVAL=$?
if [ $RETVAL -eq 0 ]
then
AUSER=`grep "\
#!/bin/bash
FILE=/etc/inittab
if [ ! -e $FILE ]
then
echo "File is not exists."
exit 0
fi
if grep "^$" $FILE &> /dev/null
then
echo "Tatal blank lines is : `grep "^$" $FILE | wc -l`"
else
echo "No blank lines"
fi
#!/bin/bash
USERNAME=root
TIMESTAMP=`date +%s`
TODAY=$[$TIMESTAMP/86400]
MAX=`grep "^root" /etc/shadow | cut -d: -f5`
CHANGE=`grep "^root" /etc/shadow | cut -d: -f3`
USE=$[$TODAY-$CHANGE]
REMAIN=$[$MAX-$USE]
WARNINGDAY=`grep "^root" /etc/shadow | cut -d: -f6`
if [ $WARNINGDAY -gt $REMAIN ]
then
echo "Warning"
else
echo "OK"
fi
[root@localhost ~]# A=3
[root@localhost ~]# B=3
[root@localhost ~]# C=$A+$B
[root@localhost ~]# echo $C
3+3
[root@localhost ~]# let C=$A+$B
[root@localhost ~]# echo $C
6
[root@localhost ~]# D=$[$A+$B]
[root@localhost ~]# echo $D
6
[root@localhost ~]# E=$(($A+$B))
[root@localhost ~]# echo $E
6
[root@localhost ~]# F=`expr $A + $B `
[root@localhost ~]# echo $F
6
#!/bin/bash
FILE=/etc/passwd
if [ ! -e $FILE ] ; then
echo "NO such file"
exit 6
fi
if [ -f $FILE ] ; then
echo "It's common file"
elif [ -d $FILE ] ; then
echo "It's a dictionary"
else
echo "I don't kown"
fi
[root@localhost shell_example]# bash -x file.sh
+ FILE=/etc/inittab
+ '[' '!' -e /etc/inittab ']'
+ grep '^$' /etc/inittab
+ echo 'No blank lines'
No blank lines
[root@localhost shell_example]# bash -x filetest.sh
+ FILE=/etc/asdf
+ '[' '!' -e /etc/asdf ']'
+ echo 'NO such file'
NO such file
+ exit 6
[root@localhost shell_example]# [ $A == $B ]
[root@localhost shell_example]# echo $?
1
[root@localhost shell_example]# [ $A = $B ]
[root@localhost shell_example]# echo $?
1
[root@localhost shell_example]# B=hello
[root@localhost shell_example]# [ $A = $B ]
[root@localhost shell_example]# echo $?
0
[root@localhost shell_example]# [ -e asdf ]
[root@localhost shell_example]# echo $?
1
[root@localhost shell_example]# [ -e ]
[root@localhost shell_example]# echo $?
0
#!/bin/bash
let SUM=0
for i in `seq 100`
do
let SUM=$[$SUM+$i]
done
echo $SUM
#!/bin/bash
LINES=`wc -l /etc/passwd | cut -d' ' -f1`
for i in `seq 1 $LINES`
do
echo "hello `head -n $i /etc/passwd | tail -1 | cut -d: -f1` and your shell is `head -n $i /etc/passwd | tail -1 | cut -d: -f7`"
done
~
#!/bin/bash
if [ $1 == 'add' ] ; then
for i in `seq 10` ; do
useradd user$i
echo "user$i" | passwd --stdin user$i &>/dev/null
done
elif [ $1 == 'del' ]; then
for i in `seq 10` ; do
userdel -r user$i
done
else
echo "Quiting......"
exit 10
fi
#!/bin/bash
declare -i sum=0
for i in `seq 100` ; do
if [ $[$i%3] -eq 0 ] ;then
sum=$[$sum+$i]
else
echo "$i not div 3"
fi
done
echo "The sum is : $sum"
#!/bin/bash
declare -i sum1=0
declare -i sum2=0
for i in `seq 0 2 100`; do
sum2=$[$sum2+$i]
done
for i in `seq 1 2 100`; do
sum1=$[$sum1+$i]
done
echo "The odd number sum is : $sum1 ."
echo "The even number sum is : $sum2 ."
declare -i bashnum=0
declare -i nologinnum=0
let num=`wc -l /etc/passwd | cut -d' ' -f1`
for i in `seq $num` ;do
username=`head -n $i /etc/passwd | tail -1 | cut -d: -f1`
shell=`head -n $i /etc/passwd | tail -1 | cut -d: -f7`
if [ `echo $shell | sed -r "s@^/.*/([^/]+/?)@\1@g" ` == 'bash' ]; then
bashnum=$[$bashnum+1]
echo $username >> /tmp/bash.txt
elif [ `echo $shell | sed -r "s@^/.*/([^/]+/?)@\1@g"` == 'nologin' ]; then
nologinnum=$[$nologinnum+1]
echo $username >> /tmp/nologin.txt
fi
done
#!/bin/bash
if ! id $1 &>/dev/null
then
echo "no such user"
exit 10
fi
if [ `id -n -u $1` == `id -n -g $1` ]
then
echo "same"
else
echo "no same"
fi
echo "scale=2;$1$2$3;" | bc
[root@localhost shell_example]# bc <<< "scale=2;5/3;"
#!/bin/bash
if [ $1 == 'q' ]
then
echo "quiting..."
exit 1
elif [ $1 == 'Q' ]
then
echo "quiting..."
exit 2
elif [ $1 == 'quit' ]
then
echo "quiting..."
exit 3
elif [ $1 == 'Quit' ]
then
echo "quiting..."
exit 4
else
echo $1
fi
#!/bin/bash
#
declare -i count=0
declare -i info=0
for i in `seq 1 $#`; do
if [ $# -gt 0 ]; then
case $1 in
-h|--help)
echo "usage: `basename $0` -h|--help -c|--count -v|--verbose"
exit 0
;;
-v|--verbose)
let info=1
shift
;;
-c|--count)
let count=1
shift
;;
*)
echo "usage: `basename $0` -h|--help -c|--count -v|--verbose"
exit 7
;;
esac
fi
done
if [ $count -eq 1 ]; then
echo "Logged users: `who | wc -l`."
if [ $info -eq 1 ]; then
echo "Logged users: `who | wc -l`. "
echo "They are: "
w
fi
fi