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库中是否支持某个函数的脚本?
Ans:
#!/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语言。