#!/bin/bash
#【程序46】
#题目:宏#define命令练习(1)
#1.程序分析:
#2.程序源代码:
#没有C语言的宏,就这么写了
TRUE=1
FALSE=0
RET_SQ=0
function SQ(){
local x=$1
RET_SQ=$((x*x))
}
again=$TRUE
while [ $again -eq $TRUE ];do
read -p "Program will stop if input value less than 50:" input
SQ $input
echo "The square for this number is $RET_SQ"
if [ "$input" -ge "50" ];then
again=$TRUE
else
again=$FALSE
fi
done
#!/bin/bash
#[47]题目:宏#define命令练习(2)
#1.程序分析:
#2.程序源代码:
##include "stdio.h"
##define exchange(a,b) { \ /*宏定义中允许包含两道衣裳命令的情形,此时必须在最右边加上"\"*/
# int t;\
# t=a;\
# a=b;\
# b=t;\
# }
#both of $1 & $2 must be global variable
########################################
#This script does not works.
########################################
function exchange(){
local x=\$"$1"
local y=\$"$2"
local xv=$(eval "expr \"$x\"")
local yv=$(eval "expr \"$y\"")
eval "$1=\"$yv\""
eval "$2=\"$xv\""
}
=10
y=20
echo "before x = $x, y = $y"
exchange $x $y
x = $x, y = $y"
#!/bin/bash
#程序48】
#题目:宏#define命令练习(3)
#1.程序分析:
#2.程序源代码:
##define LAG >
##define SMA <
##define EQ ==
##include "stdio.h"
#void main()
#{
# int i=10;
# int j=20;
# if(i LAG j)
# printf("\40: %d larger than %d \n",i,j);
# else if(i EQ j)
# printf("\40: %d equal to %d \n",i,j);
# else if(i SMA j)
# printf("\40:%d smaller than %d \n",i,j);
# else
# printf("\40: No such value.\n");
#}
#不知道如何用bash实现类似的功能
LAG="-gt"
SMA="-lt"
EQ="-eq"
i=10
j=20
eval "if [ $i $LAG $j ];then \
echo \"$i is larger than $j\"; \
elif [ $i $EQ $j ];then \
echo \"$i is equal to $j\"; \
elif [ $i $SMA $j ];then \
echo \"$i is smaller than $j\"; \
else \
echo \"No such value.\"; \
fi
"
#!/bin/bash
#【程序49】
#题目:#if #ifdef和#ifndef的综合应用。
#1. 程序分析:
#2.程序源代码:
##include "stdio.h"
##define MAX
##define MAXIMUM(x,y) (x>y)?x:y
##define MINIMUM(x,y) (x>y)?y:x
#void main()
#{
# int a=10,b=20;
##ifdef MAX
# printf("\40: The larger one is %d\n",MAXIMUM(a,b));
##else
# printf("\40: The lower one is %d\n",MINIMUM(a,b));
##endif
##ifndef MIN
# printf("\40: The lower one is %d\n",MINIMUM(a,b));
##else
# printf("\40: The larger one is %d\n",MAXIMUM(a,b));
##endif
##undef MAX
##ifdef MAX
# printf("\40: The larger one is %d\n",MAXIMUM(a,b));
##else
# printf("\40: The lower one is %d\n",MINIMUM(a,b));
##endif
##define MIN
##ifndef MIN
# printf("\40: The lower one is %d\n",MINIMUM(a,b));
##else
# printf("\40: The larger one is %d\n",MAXIMUM(a,b));
##endif
#}
MAX=
RET_MAXIMUX=
MAXIMUX(){
local x=$1
local y=$2
RET_MAXIMUX=$(( x > y ? $x : $y ))
}
MINMUX(){
local x=$1
local y=$2
RET_MIMUX=$(( x > y ? $x :$x ))
}
if [ -n MAX ];then
MAXIMUX 10 30
echo "MAX = $RET_MAXIMUX"
else
MINMUX 10 30
echo "MIN is not defined."
fi
阅读(849) | 评论(0) | 转发(0) |