系统环境变量

一、编写打印时间的程序linux

任务描述:shell

  • 编写c程序,该程序从0开始,每隔两秒输出一个整数,该整数为上一个整数+1,而且输出当前系统时间
  • 该程序无限次运行,直到外界终止该程序
  • 对上述程序改进,实现以命令参数执行打印时间的程序

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

任务描述:命令行

  • 编写c程序,该程序每次读取两个整数x、y,整数大小为1-99999,计算x+y的值,并输出到屏幕上
  • 分别使用scanf("%s",&str),gets(str),fgets(str,n,fp)来实现从标准输入流中获取参数值
  • 使用fgets实现从文件读取两个整数并相加(不要使用feof判断,会致使最后一行读两次,用fgets(s,5,fp)==NULL)
  • 该程序要求计算无数次,直到外界终止

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脚本启动和终止进程

任务描述:

  • 编写shell脚本,脚本打开三个终端,并在每一个终端中执行task1
  • 打印出每一个task1对应的进程的详细信息,至少包括user,pid,cpu,mem四个信息
  • 在脚本执行1分钟后终止全部task1对应的进程,而后再打印出此时全部进程的信息
#!/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脚本查看子进程和父进程

任务描述:

  • 编写shell脚本,脚本可以依次输出当前脚本运行的进程的pid,当前进程的命令名,当前进程父进程的pid,当前进程父进程的命令名
  • 打印进程后不自动终止,经过手动输入命令核对第一步中打印的信息,核对完成后手动终止该进程
#!/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脚本设置环境变量

任务描述:

  • 读取当前系统中的环境变量,包括当前用户名和当前用户目录名信息
  • 设置环境变量WELCOME,内容是"welcome,{用户名},Your home is{用户目录名},Today is {时间日期}"
  • 在每次用户登录时将WELCOME设为系统环境变量

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语言操做环境变量

任务描述:

  • 使用c语言读取并从新设置task5中的环境变量WELCOME
  • 读取WELCOME值
  • 从新设置WELCOME值
  • 打印设置后的WELCOME值
#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脚本中设置的环境变量

任务描述:

  • 编写脚本setenv.sh,用于设置环境变量HELLO,值为hello
  • 经过source命令设置环境变量
  • 编写脚本test,sh,读取HELLO和USER两个环境变量,打印出Hello,username!

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

任务描述:

  • 使用c语言调用系统调用gethostname,读取当前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

任务描述:

  • 打印当前主机名称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;
}
相关文章
相关标签/搜索