shell脚本的常用用法
01.sh
!/bin/bash
echo “参数
echo “参数长度#”
for param in “@”
do
echo “param:$param”
done
调用
./01.sh 1 2 3
02.sh
-d 检查文件是否存在,并且是一个目录
-e 检查file是否存在
-f 检查是否存在,并且是一个文件
-r 检查是否存在,可读
-w 检查文件是否可写
-x 是否可执行
Switch的使用 03.sh
!/bin/bash
testUser=
tim)
echo “tim”;;
david)
echo “david”;;
*)
echo “默认”;;
esac
*好等于default
每个case结束两个分号
调用
./03.sh tim
for循环04.sh
!/bin/bash
for item in Mther Father Brother
do
echo “$item”
done
for循环05.sh
!/bin/bash
Family=”Mther Father Brother”
for item in
done
While 循环06.sh
!/bin/bash
a=10
while [
if [$a -eq 5 ]
then
echo “跳出循环”
break
fi
done
写入文件 07.sh
输出
追加输出
<输入
!/bin/bash
file=test6
echo “aa”>
‘>!’:如果存在则覆盖
‘>&’:执行时屏幕上所产生的任何信息写入指定的文件中
‘>>’:追加到文件中
‘>>&’:屏幕上的信息追加到文件中
01.sh
!/bin/bash
echo “参数
echo “参数长度#”
for param in “@”
do
echo “param:$param”
done
调用
./01.sh 1 2 3
02.sh
-d 检查文件是否存在,并且是一个目录
-e 检查file是否存在
-f 检查是否存在,并且是一个文件
-r 检查是否存在,可读
-w 检查文件是否可写
-x 是否可执行
Switch的使用 03.sh
!/bin/bash
testUser=
tim)
echo “tim”;;
david)
echo “david”;;
*)
echo “默认”;;
esac
*好等于default
每个case结束两个分号
调用
./03.sh tim
for循环04.sh
!/bin/bash
for item in Mther Father Brother
do
echo “$item”
done
for循环05.sh
!/bin/bash
Family=”Mther Father Brother”
for item in
done
While 循环06.sh
!/bin/bash
a=10
while [
if [$a -eq 5 ]
then
echo “跳出循环”
break
fi
done
写入文件 07.sh
输出
追加输出
<输入
!/bin/bash
file=test6
echo “aa”>
‘>!’:如果存在则覆盖
‘>&’:执行时屏幕上所产生的任何信息写入指定的文件中
‘>>’:追加到文件中
‘>>&’:屏幕上的信息追加到文件中
找文件内容
grep -n bin 06.sh
01.sh
!/bin/bash
echo “参数
echo “参数长度#”
for param in “@”
do
echo “param:$param”
done
调用
./01.sh 1 2 3
02.sh
-d 检查文件是否存在,并且是一个目录
-e 检查file是否存在
-f 检查是否存在,并且是一个文件
-r 检查是否存在,可读
-w 检查文件是否可写
-x 是否可执行
Switch的使用 03.sh
!/bin/bash
testUser=
tim)
echo “tim”;;
david)
echo “david”;;
*)
echo “默认”;;
esac
*好等于default
每个case结束两个分号
调用
./03.sh tim
for循环04.sh
!/bin/bash
for item in Mther Father Brother
do
echo “$item”
done
for循环05.sh
!/bin/bash
Family=”Mther Father Brother”
for item in
done
While 循环06.sh
!/bin/bash
a=10
while [
if [$a -eq 5 ]
then
echo “跳出循环”
break
fi
done
写入文件 07.sh
输出
追加输出
<输入
!/bin/bash
file=test6
echo “aa”>
‘>!’:如果存在则覆盖
‘>&’:执行时屏幕上所产生的任何信息写入指定的文件中
‘>>’:追加到文件中
‘>>&’:屏幕上的信息追加到文件中
找文件内容
grep -n bin 06.sh
01.sh
!/bin/bash
echo “参数
echo “参数长度#”
for param in “@”
do
echo “param:$param”
done
调用
./01.sh 1 2 3
02.sh
-d 检查文件是否存在,并且是一个目录
-e 检查file是否存在
-f 检查是否存在,并且是一个文件
-r 检查是否存在,可读
-w 检查文件是否可写
-x 是否可执行
Switch的使用 03.sh
!/bin/bash
testUser=
tim)
echo “tim”;;
david)
echo “david”;;
*)
echo “默认”;;
esac
*好等于default
每个case结束两个分号
调用
./03.sh tim
for循环04.sh
!/bin/bash
for item in Mther Father Brother
do
echo “$item”
done
for循环05.sh
!/bin/bash
Family=”Mther Father Brother”
for item in
done
While 循环06.sh
!/bin/bash
a=10
while [
if [$a -eq 5 ]
then
echo “跳出循环”
break
fi
done
写入文件 07.sh
输出
追加输出
<输入
!/bin/bash
file=test6
echo “aa”>
‘>!’:如果存在则覆盖
‘>&’:执行时屏幕上所产生的任何信息写入指定的文件中
‘>>’:追加到文件中
‘>>&’:屏幕上的信息追加到文件中
找文件内容
grep -n bin 06.sh
01.sh
!/bin/bash
echo “参数
echo “参数长度#”
for param in “@”
do
echo “param:$param”
done
调用
./01.sh 1 2 3
02.sh
-d 检查文件是否存在,并且是一个目录
-e 检查file是否存在
-f 检查是否存在,并且是一个文件
-r 检查是否存在,可读
-w 检查文件是否可写
-x 是否可执行
Switch的使用 03.sh
!/bin/bash
testUser=
tim)
echo “tim”;;
david)
echo “david”;;
*)
echo “默认”;;
esac
*好等于default
每个case结束两个分号
调用
./03.sh tim
for循环04.sh
!/bin/bash
for item in Mther Father Brother
do
echo “$item”
done
for循环05.sh
!/bin/bash
Family=”Mther Father Brother”
for item in
done
While 循环06.sh
!/bin/bash
a=10
while [
if [$a -eq 5 ]
then
echo “跳出循环”
break
fi
done
写入文件 07.sh
输出
追加输出
<输入
!/bin/bash
file=test6
echo “aa”>
‘>!’:如果存在则覆盖
‘>&’:执行时屏幕上所产生的任何信息写入指定的文件中
‘>>’:追加到文件中
‘>>&’:屏幕上的信息追加到文件中
找文件内容
grep -n bin 06.sh