免费注册 查看新帖 |

Chinaunix

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

[系统管理] Shell脚本中的变量值的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-05-08 10:41 |只看该作者 |倒序浏览
问题描述:
我想从文本文件/etc/X11/xorg.conf 下面读取出变量 PreferredMode后的字符串,内容如下:

Section "Monitor"
       Identifier "LCD"
       Option   "Enable"  "true"
       Option                   "PreferredMode" "1280x960"
EndSection

我的脚本内容如下:
#!/bin/sh
pmode=`grep PreferredMode /etc/X11/xorg.conf | awk '{print $3}'`
echo {pmode}
xrandr --output LCD --mode ${pmode}

运行的时候,会打印出 ”1280x1024“,  但是这个pmode不能被后面的xrandr命令识别。
但是如果改成下面的直接赋值,就能正常运行。
#!/bin/sh
pmode=”1280x1024“
xrandr --output LCD --mode ${pmode}

所以看起来是mode=`grep PreferredMode /etc/X11/xorg.conf | awk '{print $3}'` 这个语句的处理结果直接赋值给pmode有问题。可能需要转换一下格式。
大家能给些建议么? 感激不尽

论坛徽章:
0
2 [报告]
发表于 2012-06-24 10:33 |只看该作者
赋值给变量的内容完全不一样啊,所以不行:
[olive@fruit tmp]$ cat d1
Section "Monitor"
       Identifier "LCD"
       Option   "Enable"  "true"
       Option                   "PreferredMode" "1280x960"
EndSection

[olive@fruit tmp]$ pmpmode=`grep PreferredMode /tmp/d1 | awk '{print $3}'`
[olive@fruit tmp]$ pmode=`grep PreferredMode /tmp/d1 | awk '{print $3}'`
[olive@fruit tmp]$ echo $pmode
"1280x960"
[olive@fruit tmp]$ pmode2="1280x960"
[olive@fruit tmp]$ echo $pmode2
1280x960

可以这样:
[olive@fruit tmp]$ pmode3=`grep PreferredMode /tmp/d1 | awk '{print $3}'|tr -d '"'`
[olive@fruit tmp]$ echo $pmode3
1280x960

论坛徽章:
0
3 [报告]
发表于 2012-06-24 15:35 |只看该作者
  1. pmode=$(awk -F \" '/PreferredMode/{print $4}' /etc/X11/xorg.conf)
复制代码

论坛徽章:
0
4 [报告]
发表于 2012-06-25 11:15 |只看该作者
谢谢楼上两位的帮助
我后来用下面的语句把双引号去掉,就可以了
preferred_mode=`grep PreferredMode /etc/X11/xorg.conf | awk '{print $3}' | sed 's/\"//g'`
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP