虽然学过Linux Shell编程,但由于编写Shell脚本的机会实在是比较少(通常在项目初期搭建开发环境时编写得比较多一些),所以一些语法久而久之就会忘了,一个简单的语法去翻书效率实在是慢,所以就个人整理了一个表格,由于我用的编程语言是C/C++,所以在编程的时候往往脑海里面首先浮现的是用C语言怎么去编写它,所以我干脆拿C语言的语法来和Shell编程的语法作个对比表,这样查起来就方便了,哈:
(注:不知道为什么从WORD中粘帖的表格在这里显示会不完整,在编辑的时候又是正常的,闷)
要实现的功能
|
C语言编程
|
Linux Shell脚本编程
|
程序/脚本的参数传递
|
int main(int argc, char** argv)
{
if (argv != 4) {
printf( “Usage: %s arg1 arg2 arg3”, argv[0] );
return 1;
}
printf(“arg1:%s/n”,argv[1]);
printf(“arg2:%s/n”,argv[2]);
printf(“arg3:%s/n”,argv[3]);
return 0;
}
|
#!/bin/sh
if [ $# -lt 3 ]; then
echo "Usage: `basename $0` arg1 arg2 arg3" >&2
exit 1
fi
echo "arg1: $1"
echo "arg2: $2"
echo "arg3: $3"
exit 0
|
int main(int argc, char** argv)
{
int i;
for (i=1; i<=argc;i++) {
printf(“arg:%s/n”,argv[i]);
}
return 0;
}
|
#!/bin/sh
while [ $# -ne 0 ]
do
echo "arg: $1"
shift
done
|
逻辑/数值运算
|
if (d == 0)
|
if [ "$D" -eq "0" ] ; then
|
if (d != 0)
|
if [ "$D" -ne "0" ] ; then
|
if (d > 0)
|
if [ "$D" -gt "0" ] ; then
|
if (d < 0)
|
if [ "$D" -lt "0" ] ; then
|
if (d <= 0)
|
if [ "$D" -le "0" ] ; then
|
if (d >= 0)
|
if [ "$D" -ge "0" ] ; then
|
字符串比较
|
if (strcmp(str,”abc”)==0) {
}
|
if [ "$STR" != "abc" ]; then
fi
|
输入和输出
|
scanf(“%d”,&D);
|
read D
|
printf( “%d”, D);
|
echo –n $D
|
printf( “%d”,D);
|
echo $D
|
printf( “Press any to continue...”);
char ch=getchar();
printf( “/nyou pressed: %c/n”, ch );
|
#!/bin/sh
getchar()
{
SAVEDTTY=`stty -g`
stty cbreak
dd if=/dev/tty bs=1 count=1 2> /dev/null
stty -cbreak
stty $SAVEDTTY
}
echo -n "Press any key to continue..."
CH=`getchar`
echo ""
echo "you pressed: $CH"
|
|
read D <&3
|
程序/脚本的控制流程
|
if (isOK) {
//1
} else if (isOK2) {
//2
} else {
//3
}
|
if [ isOK ]; then
#1
elif [ isOK2 ]; then
#2
else
#3
fi
|
switch (d)
{
case 1:
printf(“you select 1/n”);
break;
case 2:
case 3:
printf(“you select 2 or 3/n”);
break;
default:
printf(“error/n”);
break;
};
|
case $D in
1) echo "you select 1"
;;
2|3) echo "you select 2 or 3"
;;
*) echo "error"
;;
esac
|
for (int loop=1; loop<=5;loop++) {
printf( “%d”, loop);
}
|
for loop in 1 2 3 4 5
do
echo $loop
done
|
do {
sleep(5);
} while( !isRoot );
|
IS_ROOT=`who | grep root`
until [ "$IS_ROOT" ]
do
sleep 5
done
|
counter=0;
while( counter < 5 ) {
printf( “%d/n”, counter);
counter++;
}
|
COUNTER=0
while [ $COUNTER -lt 5 ]
do
echo $COUNTER
COUNTER=`expr $COUNTER + 1`
done
|
while (1) {
}
|
while :
do
done
|
break;
|
break或break n,n表示跳出n级循环
|
continue;
|
continue
|
函数与过程的定义
|
void hello()
{
printf( “hello/n” );
}
…
//函数调用
hello();
|
hello()
{
Echo “hello”
} 或者
function hello()
{
Echo “hello”
}
…
#函数调用
hello
|
函数的参数和返回值
|
int ret = doIt();
if (ret == 0) {
printf( “OK/n” );
}
|
doIt
if [ “$?” –eq 0 ] ; then
echo “OK”
fi
或者
RET = doIt
if [ “$RET” –eq “0” ] ; then
echo “OK”
fi
|
int sum(int a,int b)
{
return a+b;
}
int s = sum(1,2);
printf(“the sum is: %d/n”, s);
|
sum()
{
echo -n "`expr $1 + $2`"
}
S=`sum 1 2`
echo "the sum is: $S"
|
bool isOK() { return false; }
if (isOK) {
printf( “YES/n” );
} else {
printf( “NO/n” );
}
|
isOK()
{
return 1;
}
if isOK ; then
echo "YES"
else
echo "NO"
fi
|
分享到:
相关推荐
虽然学过Linux Shell编程,但由于编写Shell脚本的机会实在是比较少(通常在项目初期搭建开发环境时编写得比较多一些),所以一些语法久而久之就会忘了,一个简单的语法去翻书效率实在是慢,所以就个人整理了一个表格,...
hadoop shell 命令速查表,大数据 shell 使用方向,重在使用和操作,希望对大家学习和工作有所帮助。
Linux 中的 SHELL 速查 Linux 中的 SHELL 速查 Linux 中的 SHELL 速查 Linux 中的 SHELL 速查 Linux 中的 SHELL 速查
电子书籍,服务器教程,Linux,Shell速查 linux中shell速查手册chm版,小巧免费,查询Linux命令非常方便
Linuxshell大作业.pdf
Linux Shell Scripting Cookbook(3rd) 英文mobi 第3版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书
linux shell编程指南linux shell编程指南linux shell编程指南linux shell编程指南linux shell编程指南linux shell编程指南linux shell编程指南linux shell编程指南linux shell编程指南linux shell编程指南linux ...
此资源是本人对linux shell脚本的语法总结,均实际验证通过。
LINUX SHELL命令练习 题
Linux Shell脚本手册 - 第三版,. This book will take you through useful real-world recipes designed to make your daily life easier when working with the shell.
LinuxShell脚本编程实例.pdf
linux下的shell编程,本人从事linux下的工作多年,总结了一些经验,希望能够帮助新童鞋快速学习linux,快速掌握shell脚本,提高工作效率
linux shell学习资料:Linux Shell从入门到精通、Linux Shell编程艺术、Linux Shell脚本攻略
本书由浅入深、循序渐进地详细讲解了LinuxShell编程的基本知识。这些基本知识包括Shell编程的基本知识、文本处理的工具和方法、正则表达式、Linux系统知识等。 本书旨在通过理清Linux Shell编程的脉络,从基本概念...
shell语法
LinuxShell编程基础教程.pdf
Linux Shell实例精讲,大量实例shell,容易理解和实践
Linux Shell常用技巧Linux Shell常用技巧
linux shell 应用.chm,学习linux shell资料