Hot answers tagged

45

iostat is part of the sysstat package, which is able to show overall iops if desired, or show them separated by reads/writes. Run iostat with the -d flag to only show the device information page, and -x for detailed information (separate read/write stats). You can specify the device you want information for by simply adding it afterwards on the command ...


31

The problem is that those /proc files on Linux appear as text files as far as stat()/fstat() is concerned, but do not behave as such. Because it's dynamic data, you can only do one read() system call on them (for some of them at least). Doing more than one could get you two chunks of two different contents, so instead it seems a second read() on them just ...


24

bash runs the right-hand side of a pipeline in a subshell context, so changes to variables (which is what read does) are not preserved — they die when the subshell does, at the end of the command. Instead, you can use process substitution: $ read a b dump < <(echo 1 2 3 4 5) $ echo $b $a 2 1 In this case, read is running within our primary shell, ...


24

There is no stand-alone read command: instead, it is a shell built-in, and as such is documented in the man page for bash: read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...] [...] -r Backslash does not act as an escape character. The back‐ slash is considered to be part ...


22

Try something like this: $ ssh -t yourserver "$(<your_script)" The -t forces a tty allocation, $(<your_script) reads the whole file and in this cases passes the content as one argument to ssh, which will be executed by the remote user's shell. If the script needs parameters, pass them after the script: $ ssh -t yourserver "$(<your_script)" arg1 ...


22

The script reads pairs of strings from two files. In each iteration, it reads a string from file11 into lineA, and from file22 into lineB. It does this until it encounters the end of either file. The body of the loop outputs the two strings and a blank line. The <file11 means "connect/redirect standard input from file11 into this command", where "this ...


16

In modern shells like bash and zsh, you have a very useful `<<<' redirector that accepts a string as an input. So you would do while IFS= read -r line ; do echo $line; done <<< "$variable" Otherwise, you can always do echo "$variable" | while IFS= read -r line ; do echo $line; done


15

This is not a bash bug as POSIX allows both bash and ksh behaviors, leading to the unfortunate discrepancy you are observing. http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_12 Additionally, each command of a multi-command pipeline is in a subshell environment; as an extension, however, any or all commands in a pipeline may ...


15

read reads from standard input. But the standard input of the bash process is already taken by the script. Depending on the shell, either read won't read anything because the shell has already read and parsed the whole script, or read will consume unpredictable lines in the script. Simple solution: bash -c "$(wget -O - http://example.com/my-script.sh)" ...


15

Consider that && is a logical operator. It does not mean "also run this command" it means "run this command if the other succeeded". That means if the rm command fails (which will happen if any of the three directories don't exist) then the mkdir won't be executed. This does not sound like the behaviour you want; if the directories don't exist, it'...


13

What needs to be explained is that the command appeared to work, not its exit code '\n' is two characters: a backslash \ and a letter n. What you thought you needed was $'\n', which is a linefeed (but that wouldn't be right either, see below). The -d option does this: -d delim continue until the first character of DELIM is read, rather than ...


13

From help read: -t timeout time out and return failure if a complete line of input is not read within TIMEOUT seconds. The value of the TMOUT variable is the default timeout. TIMEOUT may be a fractional number. If TIMEOUT is 0, read returns immediately, without trying to read any data, returning success only if ...


12

The read command reads from its standard input stream and assigns what's read to the variable file (it's a bit more compicated than that, see long discussion here). The standard input stream is coming from the here-document redirected into the loop after done. If not given data from anywhere, it will read from the terminal, interactively. In this case ...


12

With read, -d is used to terminate the input lines (i.e. not to separate input lines). Your last "line" contains no terminator, so read returns false on EOF and the loop exits (even though the final value was read). echo '0,1,2,3,4,5' | { while read -d, i; do echo "$i"; done; echo "last value=$i"; } (Even with -d, read also uses $IFS, absorbing whitespace ...


11

read has a parameter for timeout, you can use: read -t 3 answer If you want read to wait for a single character (whole line + Enter is default), you can limit input to 1 char: read -t 3 -n 1 answer After proper input, return value will be 0, so you can check for it like this: if [ $? == 0 ]; then echo "Your answer is: $answer" else echo "Can't ...


11

The example does not set IFS within the script, because bash disallows importing IFS from the environment, according to a comment in variables.c: /* Don't allow IFS to be imported from the environment. */ temp_var = bind_variable ("IFS", " \t\n", 0); setifs (temp_var); Built-in commands and non-script uses get the assignment to IFS, of course, but ...


10

You provided a line with 6 "words", yet you're reading them into three variables: a, b, and c. The first variable a is assigned 1, the second variable b is assigned 2, and c gets to hold the rest of the line: "3 4 5 6". The output is 3 4 5 6 b a because you didn't write $c $b $a, but only $c b a. If you had written $c $b $a, the result would have been 3 4 ...


10

You can always define in your jumpbox Multiplexing in OpenSSH Multiplexing is the ability to send more than one signal over a single line or connection. With multiplexing, OpenSSH can re-use an existing TCP connection for multiple concurrent SSH sessions rather than creating a new one each time. An advantage with SSH multiplexing is that the ...


10

I get the same behaviour with: sleep 1 && true < /dev/tty & read var sudo opens /dev/tty to query the current foreground process group, that causes the read() system call made by bash's read to return with EAGAIN with Ubuntu 18.04's Linux kernel 4.15.0-45-generic and 4.18.0-14-generic at least causing the read utility to return with that ...


9

When you read a whole line with plain read (or read -r or other options that don't affect this behavior), the kernel-provided line editor recognizes the Backspace key to erase one character, as well as a very few other commands (including Return to finish the input line and send it). The shortcut keys can be configured with the stty utility. The terminal is ...


9

You must use mapfile (or its synonym readarray, which was introduced in bash 4.0): mapfile -t list <<<"$input" One read invocation only work with one line, not the entire standard input. read -a list populate the content of first line of standard in to the array list. In your case, you got bin as the only element in array `list.


9

If you are interested in knowing why? this is so, you can see the answer in the kernel sources here: if (!data || !table->maxlen || !*lenp || (*ppos && !write)) { *lenp = 0; return 0; } Basically, seeking (*ppos not 0) is not implemented for reads (!write) of sysctl values that are numbers. Whenever a read is ...


9

It is easy to check if the first line starts with #include in (GNU and AT&T) sed: sed -n '1{/^#include/p};q' file Or simplified (and POSIX compatible): sed -n '/^#include/p;q' file That will have an output only if the file contains #include in the first line. That only needs to read the first line to make the check, so it will be very fast. So, ...


8

An array could make the string parsing without the need for a temporal file. Don't forget to turn off globbing. set -f IFS=: Hosts=($HostFull) HostMain=${Hosts[0]} HostMid=${Hosts[1]} HostSub=${Hosts[2]} set +f


8

Use read -e: $ read -e -n 5 13acX read -e means that: Readline (see Command Line Editing) is used to obtain the line. When you do that, you can edit the input in any of the ways you would when writing at the regular shell prompt, including backspace, Home, and so on.


8

It's because the part where you use the vars is a new set of commands. Use this instead: head somefile | { read A B C D E FOO; echo $A $B $C $D $E $FOO; } Note that, in this syntax, there must be a space after the { and a ; (semicolon) before the }.  Also -n1 is not necessary; read only reads the first line. For better understanding, this may help you; it ...


8

You can't use the return code of read (it's zero if it gets valid, nonempty input), and you can't use its output (read doesn't print anything). But you can put multiple commands in the condition part of a while loop. The condition of a while loop can be as complex a command as you like. while read -n1 -r -p "choose [y]es|[n]o" && [[ $REPLY != q ]]; ...


8

As other answers state, -d is an end-of-line character, not a field separator. You can do IFS=, read -a fields <<< "1,2,3,4,5" for i in "${fields[@]}"; do echo "$i"; done


7

The problem is that read will try to evaluate backslash escapes entered by the user. To do what you want, you need to add the -r switch to read which tells it to leave backslash escapes un-evaluated: read -rp "Input file name: " FilePath UnixPath="${FilePath//\\//}" Additionally, your echo commands needs double quotes around the variable substitution: echo ...


7

When you pipe the output of curl into sh you're making the script text be standard input of the shell, which takes it in as commands to run. After that, there's nothing left to read. Even if it were to try, it wouldn't get anything from the terminal input, because it's not connected to it. The pipe has replaced standard input for the sh process. The next ...


Only top voted, non community-wiki answers of a minimum length are eligible