1 编写一个Shell脚本程序,从命令行输入10个数,当输入的数中包含0或者负数时,输出”error”,同时将错误信息保存到/tmp/err文件;否则,输出这个10个数的乘积。
if [ $# -ne 10 ];then
echo "please input 10 num"
exit 1
fi
ret=1
for it in $*
do
if [ $it -le 0 ];then
echo "error" | tee -p /tmp/err
exit 1
fi
ret=`expr $ret \* $it`
done
echo $ret
参考:https://zhidao.baidu.com/question/1575803447232768340.html
2 编写一个Linux C语言程序,查看当前系统的shell环境和Java环境。
#include <stdio.h>
#include <stdlib.h>
int main(){
char* shell_path = getenv("PATH");
char* java_path = getenv("JAVA_HOME");
printf("shell:%s\n",shell_path);
printf("java:%s\n",java_path);
return 0;
}
3 编写一个Linux C语言程序,接受输入一个任意长度的字符串并输出。
#include <stdio.h>
#include <stdlib.h>
#define CHUNK_SIZE 10
int main(){
char* s;
int i = 0;
int c;
s = malloc(sizeof(char)*CHUNK_SIZE);
// fail
if(s==NULL){
printf("OOM ERROR");
return 1;
}
while((c=getchar())!=EOF){
s[i]=c;
i++;
if(i%CHUNK_SIZE==0){
s = realloc(s,sizeof(char)*CHUNK_SIZE*(i/CHUNK_SIZE+1)+1);
if(s==NULL){
printf("OOM ERROR");
return 1;
}
}
}
printf("\n%s\n",s);
free(s);
return 0;
}
参考:https://zhidao.baidu.com/question/1883761988772855668.html