shell

5、内存监控工具

#free -h >> mem.log

#free -h | sed -n '2p;2q' >> mem.log

while true
do

sed -n '/1.9G\>/p' mem.log | sed '$d' | sed -n '$p' | awk '{print $1,$2,$3,$4,$5,$6,$7}'

if ($4 < 80 ) ; then
    echo "memory space is less than " >> log.txt
fi
#sed -n '/...\>/p' mem.log | sed '$d' | sed -n '$p' | awk '{print $1,$2,$3,$4}'

sleep 1

done

mem.log

Mem:          1.9G       1.3G       610M       2.3M        52M        99M
Mem:          1.9G       1.3G       609M       2.3M        52M        99M
Mem:          1.9G       1.3G       609M       2.3M        52M        99M
Mem:          1.9G       1.3G       609M       2.3M        52M        99M

 

4、shell与c交互

1) shell给c传参数就用popen
FILE* fn = popen("exec_name","r");
用fgets或者fgetc来取得"exec_name"的输出~~
具体这些函数使用方法,建议楼主自己去 Unix下面 man一下。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <strings.h>
#include <string.h>
 
char* shellcmd(char* cmd, char* buff, int size)
{
  char temp[256];
  FILE* fp = NULL;
  int offset = 0;
  int len;
   
  fp = popen(cmd, "r");
  if(fp == NULL)
  {
    return NULL;
  }
 
  while(fgets(temp, sizeof(temp), fp) != NULL)
  {
    len = strlen(temp);
    if(offset + len < size)
    {
      strcpy(buff+offset, temp);
      offset += len;
    }
    else
    {
      buff[offset] = 0;
      break;
    }
  }
   
  if(fp != NULL)
  {
    pclose(fp);
  }
 
  return buff;
}
 
int main(void)
{
  char buff[1024];
 
  memset(buff, 0, sizeof(buff));
  printf("%s", shellcmd("ls", buff, sizeof(buff)));
 
  return 0;
}

/*
    system("ls>result.txt")
    FILE *fp = fopen(result, "r")
*/


2) 将c的变量传入shell就更简单了,有6个exec函数,都可以传参数~~
或者就直接创建一个char*用system执行好了。

3、shell中脚本参数传递的两种方式

https://blog.csdn.net/sinat_36521655/article/details/79296181

2、结束shell自身

1) 如果是shell自身的结束,可以使用exit
2) 如果是要结束shell进程。可以ps -aux | grep 获取shell脚本的进程,执行
kill -9 pid

1、shell 变量自加

declare -i iv=$svnv
let iv+=1

shell中变量自增的实现方法

Linux Shell中写循环时,常常要用到变量的自增,现在总结一下整型变量自增的方法。
我所知道的,bash中,目前有五种方法:
1. i=`expr $i + 1`;
2. let i+=1;
3. ((i++));
4. i=$[$i+1];
5. i=$(( $i + 1 ))
可以实践一下,简单的实例如下:

#!/bin/bash
i=0;
while [ $i -lt 4 ];
do
   echo $i;
   i=`expr $i + 1`;
   # let i+=1;
   # ((i++));
   # i=$[$i+1];
   # i=$(( $i + 1 ))
done

另外,对于固定次数的循环,可以通过seq命令来实现,就不需要变量的自增了;实例如下:

#!/bin/bash
for j in $(seq 1 5)
do
  echo $j
done

 

转载于:https://www.cnblogs.com/dong1/p/9528041.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值