- 浏览: 4763762 次
- 性别:
- 来自: 上海
-
文章分类
最新评论
-
bzhao:
你也应该可以这样:(not tested)./rbtunnel ...
在Bash脚本中怎么关闭文件描述符? -
bzhao:
如果有A进程原代码情况下,通过如下调用,把他的子进程继承关闭则 ...
在Bash脚本中怎么关闭文件描述符? -
Master-Gao:
楼主咋没分析下源码呢?
我使用过的Linux命令之dirname - 截取给定路径的目录部分 -
jiedushi:
tail -F 就可以吧
Linux下实时跟踪log4j日志文件的bash脚本 - 增强了tail -f的功能 -
java_is_new:
新手学习了,就是不明白为一个网卡配多个ip有什么用
我使用过的Linux命令之ifconfig - 网络配置命令
Bash字符串处理(与Java对照) - 5.字符串输入(读取字符串)
In Java
Scanner类:Scanner.hasNext() & Scanner.next() & Scanner.hasNextLine() & Scanner.nextLine()
A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.
A scanning operation may block waiting for input.
The next() and hasNext() methods and their primitive-type companion methods (such as nextInt() and hasNextInt()) first skip any input that matches the delimiter pattern, and then attempt to return the next token. Both hasNext and next methods may block waiting for further input. Whether a hasNext method blocks has no connection to whether or not its associated next method will block.
下面是关于 Scanner.hasNext() 和 Scanner.next() 的例子,用于读取以空白分隔的标识符。
Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { String msg = scanner.next(); System.out.println(msg); }
下面是关于 Scanner.hasNextLine() 和 Scanner.nextLine() 的例子,用于读取输入数据行。
Scanner scanner = new Scanner(System.in); while (scanner.hasNextLine()) { String msg = scanner.nextLine(); System.out.println(msg); }
更详细的说明请参考:http://download.oracle.com/javase/1,5.0/docs/api/java/util/Scanner.html
Console类:System.console() & Console.readLine() & Console.readPassword()
Console类是JDK6加入的,它是对控制台的封装。使用 System.console() 可以获得与当前Java虚拟机关联的控制台,如果有的话。Console.readLine可以读取一行,Console.readPassword可以读取密码。
extends Object
implements Flushable
Methods to access the character-based console device, if any, associated with the current Java virtual machine.
Whether a virtual machine has a console is dependent upon the underlying platform and also upon the manner in which the virtual machine is invoked. If the virtual machine is started from an interactive command line without redirecting the standard input and output streams then its console will exist and will typically be connected to the keyboard and display from which the virtual machine was launched. If the virtual machine is started automatically, for example by a background job scheduler, then it will typically not have a console.
If this virtual machine has a console then it is represented by a unique instance of this class which can be obtained by invoking the System.console() method. If no console device is available then an invocation of that method will return null.
String readLine()
Reads a single line of text from the console.
char[] readPassword()
Reads a password or passphrase from the console with echoing disabled
下面是一个关于Console的示例代码:
Console console = System.console(); // 获得Console实例对象 if (console != null) { // 判断是否有控制台的使用权 String user = new String(console.readLine("Enter username:")); // 读取整行字符 String pwd = new String(console.readPassword("Enter passowrd:")); // 读取密码,输入时不显示 console.printf("Username is: " + user + "\n"); // 显示用户名 console.printf("Password is: " + pwd + "\n"); // 显示密码 } else { System.out.println("Console is unavailable."); // 提示无控制台使用权限 }
更详细的说明请参考:·http://download.oracle.com/javase/6/docs/api/java/io/Console.html
In Bash
从标准输入读取
使用read命令读取字符串到变量中。但是,如果有反斜杠,将起到转义的作用。\\表示一个\号,\<newline>表示续行,\<char>代表<char>本身。
格式:read VAR
格式:read -p <prompt> VAR
[root@node56 ~]# read VAR
hello world
[root@node56 ~]# echo $VAR
hello world
[root@node56 ~]# read -p "Input your number: " VAR
Input your number: 123
[root@node56 ~]# echo $VAR
123
[root@node56 ~]# read VAR
yes\tno
[root@node56 ~]# echo $VAR
yestno
[root@node56 ~]#
读取一行文本,但是取消反斜杠的转义作用。
格式:read -r VAR
格式:read -p <prompt> -r VAR
line. In particular, a backslash-newline pair may not be used as a line continuation.
[root@node56 ~]# read -r VAR
yes\tno
[root@node56 ~]# echo $VAR
yes\tno
[root@node56 ~]#
关于read -p
此参数用于在读取变量之前显示提示信息。如果输入重定向了,就不会显示,比如重定向从文件中读取。
也就是说,read -p PROMPT VAR 不等同于 echo PROMPT; read VAR 的组合。
Display prompt on standard error, without a trailing newline, before attempting to read any
input. The prompt is displayed only if input is coming from a terminal.
读取密码(输入的字符不回显)
格式:read -s PASSWORD
格式:read -p <prompt> -s PASSWORD
[root@node56 ~]# echo $PASSWORD
[root@node56 ~]# read -s PASSWORD
[root@node56 ~]# echo $PASSWORD
12345
[root@node56 ~]# read -p "Input password: " -s PASSWORD
Input password:
[root@node56 ~]# echo $PASSWORD
54321
读取指定数量字符
格式:read -n <nchars> VAR
格式:read -p <prompt> -n <nchars> VAR
read returns after reading nchars characters rather than waiting for a complete line of input.
[root@node56 ~]# read -p "Input two chars: " -n 2 VAR
Input two chars: 12
[root@node56 ~]# echo $VAR
12
[root@node56 ~]#
在指定时间内读取
格式:read -t <seconds> VAR
格式:read -p <prompt> -t <seconds> VAR
Cause read to time out and return failure if a complete line of input is not read within timeout
seconds. This option has no effect if read is not reading input from the terminal or a pipe.
[root@node56 ~]# read -p "Input some words in 5 seconds: " -t 5 VAR
Input some words in 5 seconds: [root@node56 ~]#
从文件中读取
格式:read VAR <file.txt
对于read命令,可以指定-r参数,避免\转义。
格式:read -r VAR <file.txt
错误:cat file.txt | read VAR (此处read命令将会在子进程中执行,子进程无法更改父进程的变量)
[root@web ~]# cat input.txt
Some text here
with backslash \ here
dollar $HOME meet
[root@web ~]# cat input.txt | read VAR
[root@web ~]# echo $VAR
[root@web ~]# read VAR <input.txt
[root@web ~]# echo $VAR
Some text here
[root@web ~]# read VAR1 VAR2 VAR3 VAR4<input.txt
[root@web ~]# echo "VAR1=$VAR1 VAR2=$VAR2 VAR3=$VAR3 VAR4=$VAR4"
VAR1=Some VAR2=text VAR3=here VAR4=
[root@web ~]# read VAR1 VAR2<input.txt
[root@web ~]# echo "VAR1=$VAR1 VAR2=$VAR2"
VAR1=Some VAR2=text here
[root@web ~]#
上面的直接对read命令输入重定向,只能读取输入文件中的一行。
如果需要读取整个文件,最好将其写在一个代码块中。
The code block enclosed in braces may have I/O redirected to and from it.
Unlike a command group within (parentheses), as above, a code block enclosed by {braces} will not normally launch a subshell.
{ read LINE1; read LINE2; read LINE3; } <input.txt
注意每个read命令之后都要以分号结束。
[root@web ~]# { read LINE1; read LINE2; read LINE3; } <input.txt
[root@web ~]# echo $LINE1
Some text here
[root@web ~]# echo $LINE2
with backslash here
[root@web ~]# echo $LINE3
dollar $HOME meet
[root@web ~]#
上面LINE2中\ 没有读取出来,因为在没有加上-r参数时,read会转义。
现在加上-r参数来测试一下。
{ read -r LINE1; read -r LINE2; read -r LINE3; } <input.txt
[root@web ~]# { read -r LINE1; read -r LINE2; read -r LINE3; } <input.txt
[root@web ~]# echo $LINE1
Some text here
[root@web ~]# echo $LINE2
with backslash \ here
[root@web ~]# echo $LINE3
dollar $HOME meet
[root@web ~]#
从Here Document读取
错误:read VAR <<EOF
some string
EOF
正确:{ read VAR; } <<EOF
some string
EOF
问题:为什么写在代码块中才能读取呢?
[root@web ~]# read VAR <<EOF
> some string
> EOF
[root@web ~]# echo "$VAR"
[root@web ~]# { read VAR; } <<EOF
> hello
> EOF
[root@web ~]# echo "$VAR"
hello
[root@web ~]#
从Here String读取
read VAR <<< "some string"
read VAR <<< "$STR"
[root@web ~]# read VAR <<< "some string"
[root@web ~]# echo $VAR
some string
[root@web ~]#
补充:关于 cat input.txt | read VAR 的问题
先说一下我对管道线的理解:管道线两侧的命令,前一个命令的输出(标准输出),将作为后一个命令的输入(标准输入)。两侧的命令都是在子进程中执行的,都有各自独立的地址空间,子进程会继承(复制)父进程所有的环境变量。子Shell(subshell)是一种特殊的子进程,它会继承(复制)父Shell的所有变量;对于非子Shell进程(外部命令),只有环境变量会被继承(复制)。但是,子进程一旦启动,它所有的变量只在它的进程空间中有效,它对变量的修改都是对自己范围之内的变量的修改,对父进程相同名称的变量是没有影响的。而子进程启动之后,它的父进程对父进程变量的修改,对子进程相同名称的变量也是没有影响的。
pipe. Passes the output (stdout) of a previous command to the input (stdin) of the next one, or to the shell. This is a method of chaining commands together.
A pipe, as a classic method of interprocess communication, sends the stdout of one process to the stdin of another. In a typical case, a command, such as cat or echo, pipes a stream of data to a filter, a command that transforms its input for processing.
The output of a command or commands may be piped to a script.
A pipe runs as a child process, and therefore cannot alter script variables.
variable="initial_value"
echo "new_value" | read variable
echo "variable = $variable" # variable = initial_value
[root@web ~]# cat input.txt
Some text here
with backslash \ here
dollar $HOME meet
[root@web ~]# cat input.txt | read VAR
[root@web ~]# echo $VAR
上面的cat input.txt的输出将作为read VAR的输入。
[root@web ~]# cat input.txt | { read VAR; echo $VAR; }
Some text here
可以看到管道线后面的子Shell中确实读到了值。
[root@web ~]# echo $VAR
但父Shell中的相同变量不会改变。
[root@web ~]#
本文链接:http://codingstandards.iteye.com/blog/1170586 (转载请注明出处)
返回目录:Java程序员的Bash实用指南系列之字符串处理(目录)
上节内容:Bash字符串处理(与Java对照) - 4.字符串输出
下节内容:Bash字符串处理(与Java对照) - 6.判断字符串是否为空(不为空)
评论
做了一下补充,看看能否说明问题
3ks , 理解了.
做了一下补充,看看能否说明问题
发表评论
-
Bash字符串处理(与Java对照) - 22.判断字符串是否数字串
2011-10-25 09:25 5553Bash字符串处理(与Java对照) - 22.判断字符串是否 ... -
Bash字符串处理(与Java对照) - 21.字符串正则匹配
2011-10-24 09:07 11113Bash字符串处理(与Java对照) - 21.字符串正则匹配 ... -
Bash字符串处理(与Java对照) - 20.查找子串的位置
2011-10-19 09:14 6882Bash字符串处理(与Java对照) - 20.查找子串的位置 ... -
Bash字符串处理(与Java对照) - 19.查找字符的位置
2011-10-18 09:06 5965Bash字符串处理(与Java对照) - 19.查找字符的位置 ... -
Bash字符串处理(与Java对照) - 18.格式化字符串
2011-10-17 09:18 5064Bash字符串处理(与Java对照) - 18.格式化字符串 ... -
Bash字符串处理(与Java对照) - 17.判断是否以另外的字符串结尾
2011-10-09 08:58 7044Bash字符串处理(与Java对照) - 17.判断是否以另外 ... -
Bash字符串处理(与Java对照) - 16.判断是否以另外的字符串开头
2011-10-08 09:17 8464Bash字符串处理(与Java对照) - 16.判断是否以另外 ... -
Bash字符串处理(与Java对照) - 15.计算子串出现的次数
2011-09-28 09:37 3476Bash字符串处理(与Java对照) - 15.计算子串出现的 ... -
Bash字符串处理(与Java对照) - 14.判断是否包含另外的字符串(多达6种方法)
2011-09-27 13:22 8415Bash字符串处理(与Java对照) - 14.判断是否包含另 ... -
Bash字符串处理(与Java对照) - 13.字符串数组连接(以指定分隔符合并)
2011-09-26 09:19 5269Bash字符串处理(与Java对照) - 13.字符串数组连接 ... -
Bash字符串处理(与Java对照) - 12.字符串连接
2011-09-23 09:08 6392Bash字符串处理(与Java对照) - 12.字符串连接 ... -
Bash字符串处理(与Java对照) - 11.比较两个字符串大小(字典顺序、数值比较)
2011-09-21 09:31 5768Bash字符串处理(与Java对照) - 11.比较两个字符串 ... -
Bash字符串处理(与Java对照) - 10.判断两个字符串是否相等(不等)
2011-09-20 09:16 6998Bash字符串处理(与Java对照) - 10.判断两个字符串 ... -
Bash字符串处理(与Java对照) - 9.获取字符串指定位置的字符、遍历字符串中的字符
2011-09-19 09:13 3782Bash字符串处理(与Java对照) - 9.获取字符串指定位 ... -
Bash字符串处理(与Java对照) - 8.计算字符串长度
2011-09-16 09:20 5740Bash字符串处理(与Java对照) - 8.计算字符串长度 ... -
Bash字符串处理(与Java对照) - 7.字符串与默认值
2011-09-15 09:20 4018Bash字符串处理(与Java对照) - 7.字符串与默认值 ... -
Bash字符串处理(与Java对照) - 6.判断字符串是否为空(不为空)
2011-09-14 09:20 7287Bash字符串处理(与Java对照) - 6.判断字符串是否为 ... -
Bash字符串处理(与Java对照) - 4.字符串输出
2011-09-08 09:30 3845Bash字符串处理(与Java对照) - 4.字符串输出 I ... -
Bash字符串处理(与Java对照) - 3.给(字符串)变量赋值
2011-09-07 09:29 6953Bash字符串处理(与Java ... -
Bash字符串处理(与Java对照) - 2.字符串的表示方式(字符串常量)
2011-09-06 09:18 6216Bash字符串处理(与Java ...
相关推荐
python教程学习
tdac034.pdf
veclibm111111111111
nssm工具,进行2次解压后即可
视讯镜头优化,操作不走
【数据结构】的资源,包括书籍、在线课程和网站
北京大学-DeepSeek系列-提示词工程和落地场景.pdf.png
【财信国际经济研究院-2025研报】2025年2月CPI和PPI数据点评:通胀低于预期,内需亟待提振.pdf
【毕业设计】java-springboot+vue精品在线试题库系统源码(完整前后端+mysql+说明文档+LunW).zip
python安装-25.求 a 的 b 次方——有点不好意思哈.py
代码实现了自适应IMM(交互式多模型)算法,专注于对目标状态进行估计,并结合了匀速(CV)和匀加速(CA)运动模型。它使用自适应观测噪声估计和扩展卡尔曼滤波器(EKF)作为滤波主体,并在最后提供误差统计特性输出。
中产品工业物联网联网的基础功能,是企业开启数字化转型的入门之选。它能帮助企业快速搭建起基础的设备数据采集与监控体系,以较低成本实
北京大学DeepSeek系列-DeepSeek与AIGC应用.pdf.png
【毕业设计-java】springboot-vue基于多维分类的知识管理系统实现源码(完整前后端+mysql+说明文档+LunW).zip
## 一、数据介绍 本数据参考C刊《管理评论》佟岩(2024)老师的做法,使用各年度省级政府工作报告中“减碳”关键词的词频总数来测度地方政府对“减碳”的重视程度,“减碳”关键词包括二氧化碳、低碳、减排、节能、能耗、环境保护(环保)、生态、绿色。 本数据包含:原始数据、参考文献、代码do文件、最终结果。 ## 二、参考文献: 佟岩,李鑫,徐国铨.企业集团碳减排压力与债务分布-来自高耗能上市公司的经验证据[J].管理评论,2024,36(02):210-221.DOI:10.14120/j.cnki.cn11-5057/f.2024.02.014 ## 三、相关数据:城市、城市编码、年份、减碳重视程度词频。
python教程学习
jfinal-undertow 用于开发、部署由 jfinal 开发的 web 项目
python学习一些项目和资源
锐捷设备命令最全大合集
uniapp-machine-learning-from-scratch-05.rar