如何使用shell获取变量名

清钟沁桐 2011-02-24 01:26:36
all_inasmfiles="all.s ./com/com.s"
cpus_inasmfiles=" "
dsps_inasmfiles=" "
cpusgroup0_inasmfiles="cpug0.s "
cpusgroup1_inasmfiles=" "
dspsgroup0_inasmfiles="dsps/dspg0.s cpug0.s"
dspsgroup1_inasmfiles=" "
dspsgroup2_inasmfiles=" "
dspsgroup3_inasmfiles="aa.c ddd.c ddd.c "

如果有个文件如上记录,我怎么能分析这个文件,并获取变量名all_inasmfiles之类的名称呢 ?
...全文
198 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
清钟沁桐 2011-02-25
  • 打赏
  • 举报
回复
刚才使用for i in `cat test.txt`;do echo $i;done尝试

发现文件中有空格,则for i in `cat test.txt`;do echo $i;done会将空格后的字符成为另外一行
有什么办法让这个空格后的字符串不另外写一行吗 ?
清钟沁桐 2011-02-25
  • 打赏
  • 举报
回复
用什么代表每行的输入内容?
清钟沁桐 2011-02-25
  • 打赏
  • 举报
回复
zyd@HCC:~/projects/ddd> cat test.txt | cut -d "_" -f1
all
cpus
dsps
cpusgroup0
cpusgroup1
dspsgroup0
dspsgroup1
dspsgroup2
dspsgroup3
这里-f1就表示获取以"_"分割的第一个域值
javacode007 2011-02-24
  • 打赏
  • 举报
回复
每读一行,查找"-"索引位置,把该行的字符串从头到"-"的索引位置取子字符串,分别追加到一个变量中,就是最终结果啦。时间不早了,睡觉先。。。
清钟沁桐 2011-02-24
  • 打赏
  • 举报
回复
所有的以下信息记录在一个文件中
all_inasmfiles="all.s ./com/com.s"
cpus_inasmfiles=" "
dsps_inasmfiles=" "
cpusgroup0_inasmfiles="cpug0.s "
cpusgroup1_inasmfiles=" "
dspsgroup0_inasmfiles="dsps/dspg0.s cpug0.s"
dspsgroup1_inasmfiles=" "
dspsgroup2_inasmfiles=" "
dspsgroup3_inasmfiles="aa.c ddd.c ddd.c "
我想解析这个文件后,得到
group="all cpus dsps cpusgroup0 cpusgroup1 dspsgroup0 dspsgroup1 dspsgroup2 dspsgroup3"
这样的信息,我该怎么操作呢 ?
iambic 2011-02-24
  • 打赏
  • 举报
回复
cut -d= -f1 your_file_name
看着奢扣 2011-02-24
  • 打赏
  • 举报
回复
split 拿等号分一下

37,722

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧