问题集锦(56-57)

Problem 56 awk代码实例

 

awk '# factorial: return factorial of user-supplied number

BEGIN {

# prompt user; use printf, not print, to avoid the newline

printf("Enter number: ")

}

# check that user enters a number

$1 ~ /^[0-9]+$/ {

# assign value of $1 to number & fact

number = $1

if (number == 0)

fact = 1

else

fact = number

# loop to multiply fact*x until x = 1

for (x = number - 1; x > 1; x--)

fact *= x

printf("The factorial of %d is %g/n", number, fact)

# exit -- saves user from typing CRTL-D.

exit

}

# if not a number, prompt again.

{ printf("/nInvalid entry. Enter a number: ")

}' -

 

 

 

awk '# lookup -- reads local glossary file and prompts user for query

#0

BEGIN { FS = "/t"; OFS = "/t"

# prompt user

printf("Enter a glossary term: ")

}

#1 read local file named glossary

FILENAME == "glossary" {

# load each glossary entry into an array

entry[$1] = $2

next

}

#2 scan for command to exit program

$0 ~ /^(quit|[qQ]|exit|[Xx])$/ { exit }

#3 process any non-empty line

$0 != "" {

if ( $0 in entry ) {

# it is there, print definition

print entry[$0]

} else

print $0 " not found"

}

#4 prompt user again for another term

{

printf("Enter another glossary term (q to quit): ")

}' glossary -

 

 

Problem 57 检测C库中是否支持某个函数的脚本?

ns:

#!/bin/sh

# Needed for systems without gettext

gcc -xc -o /dev/null - > /dev/null 2>&1 << EOF

#include <libintl.h>

int main()

{

    gettext("");

    return 0;

}

EOF

if [ ! "$?" -eq "0"  ]; then

    echo -DKBUILD_NO_NLS;

fi

选项x指定特定语言, 这里指定的是C语言。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值