前言:在编写sh脚本的时候,发现使用read命令和预期的效果不一样,,因为shell用的是zsh,查了一下发现bash和zsh中read命令的用法有些区别,这里记录一下。 读取字符 从标准输入中读取一个或多个字符 bash: read -n num input zsh: read -k num in
read: read [-ers] [-a array] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...] Read a line from the standard input and split it into fields. Reads a single line from the standard input, or from file descriptor FD if the -u...
# read password <<< "test" # 读取test作为密码放到变量password中 # echo $password test 下面是一个脚本(假设名字为redirect.sh),将某个文件作为标准输入,用描述符6作为备份描述符。 #!/usr/bin/env bash # Redirecting stdin using 'exec'. # Link file descriptor #6 with stdin. # Saves stdin. #...
可见实现1比较快捷,其主要用到了read,关于read的用法还有挺多,详见man bash。 read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...] One line is read from the standard input, or from the file descriptor fd suppli...
-s silent Disable echo of characters read onto terminal -t timout Waits for certain amount of time before exitting -u file descriptor Reads input from file descriptor specified#!/bin/bash read -p 'Enter your name: ' name echo Hello, "$name"! read -sp 'Enter Password: ' password if [...
#! /bin/bash while read line do echo "$line" done < "${1:-/dev/stdin}" 如果在命令行中直接输入./test.sh, 不给1赋值, 则交互端自动生成一个文件,等待用户输入,如果在./test.sh inputfile, 后给1赋值为inputfile,则从inputfile文件中读取输入内容。
tee – read from standard input and write to standard output and files tee [OPTION]… [FILE]… -a:使用追加输出,而非覆盖; COMMAND | tee /PATH/TO/SOMEFILE 1 # cat test|tee /tmp/test.cat 2 Hello World 3 # cat /tmp/test.cat ...
Open up you favorite text editor and a create file called hello_world.sh. Insert the following lines to a file: NOTE:Every bash shell script in this tutorial starts withshebang:"#!"which is not read as a comment. First line is also a place where you put your interpreter which is in...
In this script, we read the contents of input.txt using the stdin file descriptor (<). Then, we iterate through each line and check for the occurrence of “error“. If it contains “error“, we send the output of echo to the stderr stream using >&2. In the same way, we can wri...
Specify pubkey files (default: ~/.ssh/id_rsa.pub) or read from standard input for piping from adjacent tools github_ssh_delete_public_keys.sh - deletes given SSH keys from the currently authenticated GitHub account by key id or title regex match github_gpg_get_user_public_keys.sh - ...