Shell----chomp的使用介绍

龚志文
2023-12-01

例子:

#!/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

 类似资料: