一、編寫打印時間的程序linux
任務描述:shell
main.c:bash
#include<stdio.h> #include<stdlib.h> int main(void) { int i; for(i=0;;i++){ printf("%d\n",i); sleep(2); system("date"); } return 0; }
改進main1.c:函數
#include<stdio.h> #include<stdlib.h> #include<time.h> int main(int argc,char *argv[]) { int i; if(argc!=2){ return 0;
printf("error"); } for(i=0;;i++){ printf("%d\n",i); sleep(2); system(argv[1]); } return 0; }
二、編寫計算加法的程序spa
任務描述:命令行
main1.c(使用scanf):指針
#include<stdio.h> int main(void) { int i,x,y,z; while(1){ printf("please input x:"); scanf("%d",&x); printf("please input y:"); scanf("%d",&y); z=x+y; printf("%d+%d=%d\n",x,y,z); } return 0; }
main2.c(使用gets):code
#include<stdio.h> #include<stdlib.h> int main(void) { while(1){ int a,b,c; char x[10],y[10]; printf("please input x:"); gets(x); printf("please input y:"); gets(y); a=atoi(x); b=atoi(y); c=a+b; printf("%d+%d=%d\n",a,b,c); } return 0; }
main3.c(使用fgets):blog
fgets函數用來從文件中讀入字符串。fgets函數的調用形式以下:fgets(str,n,fp);此處,fp是文件指針;str是存放在字符串的起始地址;n是一個int類型變量。函數的功能是從fp所指文件中讀入n-1個字符放入str爲起始地址的空間內;若是在未讀滿n-1個字符之時,已讀到一個換行符或一個EOF(文件結束標誌),則結束本次讀操做,讀入的字符串中最後包含讀到的換行符。所以,確切地說,調用fgets函數時,最多隻能讀入n-1個字符。讀入結束後,系統將自動在最後加'\0',並以str做爲函數值返回。進程
#include<stdio.h> #include<stdlib.h> int main(void) { int a,b,c;char x[10],y[10]; while(1){ printf("please input x:"); fgets(x,11,stdin); printf("please inpue y:"); fgets(y,11,stdin); a=atoi(x); b=atoi(y); c=a+b; printf("%d+%d=%d\n",a,b,c); } return 0; }
main4.c:
#include<stdio.h> #include<stdlib.h> int main(void) { int a,b,c; char str1[20],str2[20]; FILE *fp; fp=fopen("num.txt","r"); while(fgets(str1,5,fp)!=NULL){ fgets(str2,5,fp); printf("%d",sizeof(str2)); a=atoi(str1); b=atoi(str2); c=a+b; printf("%d+%d=%d\n",a,b,c); } fclose(fp); return 0; }
num.txt:
123 456 789 123
三、shell腳本啓動和終止進程
任務描述:
#!/bin/bash gnome-terminal -t "1" -x bash -c "../1/main;exec bash" gnome-terminal -t "2" -x bash -c "../1/main;exec bash" gnome-terminal -t "3" -x bash -c "../1/main;exec bash" ps aux | grep "bash -c ../1/main" | awk 'NR!=4 {print $1,$2,$3,$4}' sleep 60 for a in `ps aux | grep "bash -c ../1/main" | awk 'NR!=4 {print $2}'` do kill $a done ps -a #-t 爲打開終端的標題,便於區分 #-x 後面的爲要在打開的終端中執行的腳本,根據須要本身修改就好了 # exec bash是讓打開的終端在執行完腳本後不關閉
四、shell腳本查看子進程和父進程
任務描述:
#!/bin/bash echo $$ a=`cat /proc/$$/cmdline` echo $a echo $PPID b=`cat /proc/$PPID/cmdline` echo $b read -p "do you want to end it?(y or n)" x while [ "$x" != y ] do read -p "please input y :" x done kill $$
五、shell腳本設置環境變量
任務描述:
Shell Script 是一種弱類型語言,使用變量的時候無需首先聲明其類型。新的變量會在本地數據區分配內存進行存儲,這個變量歸當前的 Shell 全部,任何子進程都不能訪問本地變
量。這些變量與環境變量不一樣,環境變量被存儲在另外一內存區,叫作用戶環境區,這塊內存中的變量能夠被子進程訪問。
env 用於顯示用戶環境區中的變量及其取值;set 用於顯示本地數據區和用戶環境區中的變量及其取值;unset 用於刪除指定變量當前的取值,該值將被指定爲 NULL;export 命令用
於將本地數據區中的變量轉移到用戶環境區。
環境變量是和 Shell 緊密相關的,用戶登陸系統後就啓動了一個 Shell。對於 Linux 來講通常是 bash,但也能夠從新設定或切換到其它的 Shell(使用 chsh 命令)。根據發行版本的狀況,bash 有兩個基本的系統級配置文件:/etc/bashrc 和/etc/profile。這些配置文件包含兩組不一樣的變量: shell 變量和環境變量。前者只是在特定的 shell 中固定(如bash),後者在不一樣 shell 中固定。很明顯,shell 變量是局部的,而環境變量是全局的。環境變量是經過 Shell 命令來設置的,設置好的環境變量又能夠被全部當前用戶所運行的程序所使用。對於 bash 這個 Shell 程序來講,能夠經過變量名來訪問相應的環境變量,經過 export來設置環境變量。
1.Linux 的變量種類
按變量的生存週期來劃分,Linux 變量可分爲兩類:
①永久的:須要修改配置文件,變量永久生效。
② 臨時的:使用 export 命令行聲明便可,變量在關閉 shell 時失效。
2.設置變量的三種方法
①在/etc/profile 文件中添加變量【對全部用戶生效(永久的)】
用 VI 在文件/etc/profile 文件中增長變量,該變量將會對 Linux 下全部用戶有效,而且是「永
久的」。
例如:編輯/etc/profile 文件,添加 CLASSPATH 變量
# vi /etc/profile
export CLASSPATH=./JAVA_HOME/lib;$JAVA_HOME/jre/lib
注:修改文件後要想立刻生效還要運行# source /etc/profile 否則只能在下次重進此用戶時生
效。
②在用戶目錄下的.bash_profile 文件中增長變量【對單一用戶生效(永久的)】
用 VI 在用戶目錄下的.bash_profile 文件中增長變量,改變量僅會對當前用戶有效,而且是「永
久的」。
例如:編輯 guok 用戶目錄(/home/guok)下的.bash_profile
$ vi /home/guok/.bash.profile
添加以下內容:
export CLASSPATH=./JAVA_HOME/lib;$JAVA_HOME/jre/lib
注:修改文件後要想立刻生效還要運行$ source /home/guok/.bash_profile 否則只能在下次重進此用戶時生效。
③直接運行 export 命令定義變量【只對當前 shell(BASH)有效(臨時的)】
在 shell 的命令行下直接使用[export 變量名=變量值]定義變量,該變量只在當前的 shell(BASH)或其子 shell(BASH)下是有效的,shell 關閉了,變量也就失效了,再打開新
shell 時就沒有這個變量,須要使用的話還須要從新定義。
#!/bin/bash echo $HOME echo $USER echo 'export WELCOME="Welcome,$USER Your home is $HOME Today is `date`" '>> ~/.profile echo $WELCOME
六、使用c語言操做環境變量
任務描述:
#include <stdio.h> #include<stdlib.h> int main(void) { char *p;
/*取得參數WELCOME環境變量的內容,參數WELCOME爲環境變量的名稱*/ if((p=getenv("WELCOME"))) printf("WELCOME=%s\n",p);
/*改變或增長環境變量的內容*/ setenv("WELCOME","hello",1); printf("WELCOME=%s\n",getenv("WELCOME")); return 0; }
七、使用其餘shell腳本中設置的環境變量
任務描述:
source 命令的做用就是用來執行一個腳本,那麼source a.sh 同直接執行 ./a.sh 有什麼不一樣呢,好比你在一個腳本里 export KKK=111 ,若是你用./a.sh 執行該腳本,執行完畢後,你運行 echo $KKK ,發現沒有值,若是你用 source 來執行 ,而後再 echo ,就會發現 KKK=111。由於調用./a.sh 來執行 shell 是在一個子 shell 裏運行的,因此執行後,結構並無反應到父 shell 裏,可是source就是在本 shell 中執行的,因此能夠看到結果。
setenv.sh:
#!/bin/bash export HELLO="Hello" >>~/.profile
test.sh:
#!/bin/bash echo ""$HELLO","$USER"!"
八、使用linux系統調用函數讀取hostname
任務描述:
#include<stdio.h> #include<sys/types.h> #include<unistd.h> #include<stdlib.h> int main(void) { char name[50]; if(gethostname(name,sizeof(name))!=0){ perror("gethostname error\n"); exit(1); } printf("hostname=%s\n",name); return 0; }
九、使用linux系統調用函數設置hostname
任務描述:
#include<stdio.h> #include<sys/types.h> #include<unistd.h> #include<stdlib.h> int main(void) { char name[50]; if(gethostname(name,sizeof(name))!=0){ perror("gethostname error\n"); exit(1); } printf("hostname=%s\n",name); char newname[50]="weijing"; if(sethostname(newname,sizeof(newname))!=0){ perror("set error\n"); exit(1); } printf("newhostname is %s\n",newname); return 0; }