免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2044 | 回复: 6
打印 上一主题 下一主题

shell脚本中字符串处理,能精确到字节吗?菜鸟问题,请帮忙 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-10-20 10:32 |只看该作者 |倒序浏览
我想在shell脚本中判断当前Linux系统内核版本:
kernel_version=`uname -r`
然后我想判断一下$kernel_version的前3个字符,到底是2.4还是2.6,怎么办?

抱歉问题很菜

论坛徽章:
0
2 [报告]
发表于 2005-10-20 10:37 |只看该作者

shell脚本中字符串处理,能精确到字节吗?菜鸟问题,请帮忙

uname -r|cut -b-3

论坛徽章:
0
3 [报告]
发表于 2005-10-20 10:43 |只看该作者

shell脚本中字符串处理,能精确到字节吗?菜鸟问题,请帮忙

[quote]原帖由 "bleem1998"]uname -r|cut -b-3[/quote 发表:


谢谢bleem1998兄,bow

偶再请教一下,我还要判断内核编译时一个CAPABILITIES的编译选项,可能是这样的:
CONFIG_SECURITY_CAPABILITIES=m
CONFIG_SECURITY_CAPABILITIES=y
# CONFIG_SECURITY_CAPABILITIES is not set

现在我想在shell脚本中判断,可是这3种可能的串长度不一样,改怎么做呢?

bow again

论坛徽章:
0
4 [报告]
发表于 2005-10-20 11:01 |只看该作者

shell脚本中字符串处理,能精确到字节吗?菜鸟问题,请帮忙


  1. #/bin/bash

  2. yy=`grep CONFIG_SECURITY_CAPABILITIES .config`
  3. xx=`echo $yy | cut -b1`
  4. if [ $xx = '#' ]; then
  5.         echo 'Not defined'
  6. else
  7.         xx=`echo $yy | cut -d= -f2`
  8.         if [ $xx = 'm' ]; then
  9.                 echo 'Module'
  10.         else
  11.                 echo 'Build in'
  12.         fi
  13. fi
复制代码

论坛徽章:
0
5 [报告]
发表于 2005-10-20 11:55 |只看该作者

shell脚本中字符串处理,能精确到字节吗?菜鸟问题,请帮忙

谢谢解答,我参照你的代码写了这样的脚本:
  1.     1 #! /bin/sh
  2.       2
  3.       3 full=`uname -r`
  4.       4 kernel_version=`uname -r|cut -b -3`
  5.       5 if [ $kernel_version = 2.6 ]
  6.       6 then
  7.       7         echo "kernel version is $full, O.K."
  8.       8 else
  9.       9         echo "Error! your Linux kernel version is NOT 2.6, please refer to"
  10.      10         echo "docs/KERNEL-RECOMPILE and re-compile the kernel"
  11.      11         exit -1
  12.      12 fi
  13.      13 yy_capability=`grep CAPABILITIES /lib/modules/$full/build/.config`
  14.      14 xx_capability=`echo $yy_capability|cut -b1`
  15.      15 if [ $xx_capability = '#' ];then
  16.      16         echo 'CAPABILITIES is not set'
  17.      17         echo "Error! your Linux kernel NOT compiled with CAPABILITIES=m, please refer to"
  18.      18         echo "docs/KERNEL-RECOMPILE and re-compile the kernel"
  19.      19         exit -1
  20.      20 else
  21.      21         xx_capability=`echo $yy_capability | cut -d= -f2`
  22.      22         if[ $xx_capability = 'm' ];then
  23.      23                 echo 'O.K, CAPABILITIES is compiled as a Module.'
  24.      24         else
  25.      25                 echo 'CAPABILITIES is built-in...'
  26.      26                 echo "Error! your Linux kernel NOT compiled with CAPABILITIES=m, please refer to"
  27.      27                 echo "docs/KERNEL-RECOMPILE and re-compile the kernel"
  28.      28                 exit -1
  29.      29         fi
  30.      30 fi
  31.      31
复制代码


可是运行时出现了:
[root@localhost ~]# ./aa.sh
kernel version is 2.6.12, O.K.
./aa.sh: line 22: syntax error near unexpected token `then'
./aa.sh: line 22: `     if[ $xx_capability = 'm' ];then'


这是什么原因呢?

论坛徽章:
0
6 [报告]
发表于 2005-10-20 12:26 |只看该作者

shell脚本中字符串处理,能精确到字节吗?菜鸟问题,请帮忙

少了个空格?

论坛徽章:
0
7 [报告]
发表于 2005-10-20 12:54 |只看该作者

shell脚本中字符串处理,能精确到字节吗?菜鸟问题,请帮忙

[quote]原帖由 "bleem1998"]少了个空格?[/quote 发表:

3Q!     
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP