Shell----chomp的使用介绍

例子:

#!/bin/perl
print "Please input an string and a number by order!\n";  
$the_string=<>;  
$the_numb=<>;  
print "The result is \n";  
print "$the_string"x"$the_numb"; 

结果:
The result is  
my 
my 
my 
my 
my

这里的问题便是没有使用chomp引起的。

来看加入chomp的情况:

#!/bin/perl
print "Please input an string and a number by order!\n";  
chomp($the_string=<>);  
chomp($the_numb=<>);  
print "The result is \n";  
print "$the_string"x"$the_numb"; 

结果:
The result is  
mymymymymy

如果字符串结尾有换行符,chomp 可以去掉它。这基本上就是它能完成的所有功能。

 

如果在shell中获取设备驱动文件的主设备号,实例如下:

$major_string=`grep -w $module /proc/devices | cut -f1 -d" "`;
chomp($major_string);
$major=$major_string;

 

 

 

转载于:https://www.jb51.net/article/33967.htm


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部