免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 767 | 回复: 0
打印 上一主题 下一主题

linux脚本 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-05-12 23:19 |只看该作者 |倒序浏览

要实现的功能
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; iargc;i++) {
printf(“arg:%s\n”,argv);
}
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
if [ "$D" -lt "0" ] ; then
if (d
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
程序/脚本的控制流程
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
     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
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


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/93992/showart_1926392.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP