Questions tagged [read]

`read` is a shell utility used for reading strings from the standard input stream, or from a file descriptor.

Filter by
Sorted by
Tagged with
31
votes
1answer
105k views

How to get total read and total write IOPS in Linux?

How do I get read and write IOPS separately in Linux, using command line or in a programmatic way? I have installed sysstat package. Please tell me how do I calculate these separately using sysstat ...
19
votes
4answers
1k views

Why some shells `read` builtin fail to read the whole line from file in `/proc`?

In some Bourne-like shells, the read builtin can not read the whole line from file in /proc (the command below should be run in zsh, replace $=shell with $shell with other shells): $ for shell in ...
19
votes
2answers
7k views

in bash, read after a pipe is not setting values

Edit : original title was "read fails in bash" With ksh I'm using read as a convenient way to separate values: $ echo 1 2 3 4 5 | read a b dump $ echo $b $a 2 1 $ But it fails in bash : $ echo 1 ...
19
votes
2answers
28k views

What is the meaning of read -r ?

in order to understand another answer: find / -type d -print0 | while read -r -d ''; do ls -ltr "$dir" | sed '$!d' the first step is to understand the usage of the option -r of the read command. ...
16
votes
4answers
15k views

Bash: interactive remote prompt

I have a script which connects to a remote server and check if some package is installed: ssh root@server 'bash -s' < myscript.sh myscript.sh: OUT=`rpm -qa | grep ntpdate` if [ "$OUT" != "" ] ; ...
16
votes
1answer
1k views

What is the meaning of “<&3” and “done < file11 3< file22”

I have the following script: while read lineA && read lineB <&3 do echo "$lineA" echo "$lineB" echo done < file11 3< file22 This code is working fine. But I don't ...
16
votes
1answer
19k views

Iterating over multiple-line string stored in variable

I read that it is bad to write things like for line in $(command), the correct way seem to be instead: command | while IFS= read -r line; do echo $line; done This works great. But what if what I ...
11
votes
3answers
679 views

bash: Some issue when using read <<<“$VARIABLE” on a read-only root partition. Any known workarounds?

Just by coincidence I had to use my ATA-ID-to-device-name script (found here: https://serverfault.com/questions/244944/linux-ata-errors-translating-to-a-device-name/426561#426561) on a read-only / ...
10
votes
3answers
6k views

Piping a script with “read” to bash

I need to run a script by piping it through bash with wget(rather than running it directly with bash). $ wget -O - http://example.com/my-script.sh | bash It's not working because my script has read ...
9
votes
4answers
16k views

Multiple commands during an SSH inside an SSH session

I have a local machine which is supposed to make an SSH session to a remote master machine and then another inner SSH session from the master to each of some remote slaves, and then execute 2 commands ...
9
votes
2answers
3k views

How to read user input when using script in pipe

General problem I want to write a script that interacts with the user even though it is in the middle of a chain of pipes. Concrete example Concretely, it takes a file or stdin, displays lines (...
8
votes
4answers
691 views

Bash Read: Reading comma separated list, last element is missed

The output of the command below is weird to me. Why does it not give me back element 5? $ echo '0,1,2,3,4,5' | while read -d, i; do echo $i; done 0 1 2 3 4 I would expect '5' to be returned as well. ...
8
votes
3answers
9k views

How to handle backspace while reading?

How do I handle the backspaces entered, it shows ^? if tried & how read counts the characters, as in 12^?3 already 5 characters were complete(though all of them were not actual input), but after ...
8
votes
2answers
4k views

read -a array -d '\n' < foo, exit code 1

If I try to execute read -a fooArr -d '\n' < bar the exit code is 1 -- even though it accomplishes what I want it to; put each line of bar in an element of the array fooArr (using bash 4.2.37). ...
7
votes
3answers
8k views

Reading stdin into a bash array

I want to accomplish the equivalent of: list=() while read i; do list+=("$i") done <<<"$input" with IFS=$'\n' read -r -a list <<<"$input" What am I doing wrong? input=`/bin/ls ...
7
votes
4answers
8k views

Is there a difference between read, head -1, and sed 1q?

The following commands seem to be roughly equivalent: read varname varname=$(head -1) varname=$(sed 1q) One difference is that read is a shell builtin while head and sed aren't. Besides that, is ...
7
votes
2answers
3k views

A while loop and an here-document - what happens when?

I have this while loop and here-document combo which I run in Bash 4.3.48(1) and I don't understand its logic at all. while read file; do source ~/unwe/"$file" done <<-EOF x.sh y.sh EOF ...
7
votes
1answer
840 views

Setting a IFS for a bash script

The typical case is IFS= read. The subject is very well explained in this thread: why is setting a variable before a command legal in bash? In order to have a complete picture, I'd be very grateful if ...
7
votes
3answers
4k views

Use bash's read builtin without a while loop

I'm used to bash's builtin read function in while loops, e.g.: echo "0 1 1 1 1 2 2 3" |\ while read A B; do echo $A + $B | bc; done I've been working on some make project, and ...
7
votes
1answer
283 views

Bash: how can I run `sudo -n true` in the background without interfering with `read`?

I have a long running Bash script that I don't want to run as root, but it needs root access periodically throughout. I solved this by asking for the user for the root password using sudo -v and ...
7
votes
2answers
6k views

How to read the user input line by line until Ctrl+D and include the line where Ctrl+D was typed

This script takes the user input line after line, and executes myfunction on every line #!/bin/bash SENTENCE="" while read word do myfunction $word" done echo $SENTENCE To stop the input, the ...
6
votes
4answers
4k views

how to check if the first line of file contain a specific string? [duplicate]

I need to write a shell script that find and print all files in a directory which starts with the string: #include. Now, I know how to check if a string is in the file, by using: for f in `ls`; do ...
6
votes
2answers
9k views

bash: non blocking read inside a loop

I have a following small bash script var=a while true : do echo $var sleep 0.5 read -n 1 -s var done It just prints the character entered by the user and waits for the next input. What ...
6
votes
3answers
3k views

Use read as a prompt inside a while loop driven by read?

I have a use case where I need to read in multiple variables at the start of each iteration and read in an input from the user into the loop. Possible paths to solution which I do not know how to ...
6
votes
2answers
4k views

Why does the read command not take interactive options when run by sh?

I want to download a provisioning script that reads some configuration parameters via read, and execute it: curl http://example.com/provisioning.sh | sh The problem is, that the read command in the ...
6
votes
2answers
717 views

What does `while read -r line || [[ -n $line ]]` mean?

I found some code for reading input from a file a while ago, I believe from Stack Exchange, that I was able to adapt for my needs: while read -r line || [[ -n "$line" ]]; do if [[ $line != "" ]] ...
6
votes
3answers
236 views

Succinct way to respond to user input?

Of course you can do this sort of thing, read var; if [[ $var = 'y' ]]; then echo "Yes"; fi But is there any way to skip the first step and do something a little more like this, (ideally without ...
6
votes
3answers
13k views

Read line from file then delete

How can I read a series of lines from a file and then remove each line? Obviously I can read like this: while read -r line; do echo $line done < myfile.txt But if I do this: while read -r ...
5
votes
4answers
5k views

Reading multiple files one line at a time in bash

I know the basic way of reading from a command in bash: cal | while IFS= read -r line ; do echo X${line}X done But what if I want to read one line from several files/commands in a loop? I've ...
5
votes
2answers
1k views

Print currently pressed keys to stdout and read them line by line

I want to know which keys are pressed on my keyboard and print the information to stdout. A tool that can do this is showkey. However, if I want to pass the data of showkey to read: while read line; ...
5
votes
1answer
1k views

How to signal end-of-input to “read -N”?

I've been trying to figure out why I get a literal end-of-transmission character (EOT, ASCII code 4) in my variable if I read Ctrl+D with read -N 1 in bash and ksh93. I'm aware of the distinction ...
5
votes
1answer
874 views

“no coprocess” error when using read [duplicate]

I have a bash script that is named reader. It reads user input: #!/bin/bash read -p "What is your name?" username echo "Hello, ${username}" Running the script by source reader (EDIT: from the zsh ...
5
votes
2answers
890 views

What's the zsh way to read n characters from stdin?

In bash, if I wanted to read, say 3, characters from a pipe, I could do: ... | read -n3 In zsh's read, the closest option seems to be -k: -k [ num ] Read only one (or num) characters. All are ...
4
votes
1answer
5k views

BASH function to read user input OR interrupt on timeout

I am trying to write a BASH function that will do x if program a or b finish. example: echomessage() { echo "here's your message" if [[sleep 3 || read -p "$*"]] then clear fi } In this scenario: ...
4
votes
3answers
49k views

Read lines and match against pattern

I can't figure this out. I need to look at every line in file and check out whether it is matching word which is given in variable I started through command read, but I don't know what am I supposed ...
4
votes
1answer
5k views

Read space as input in shell script [duplicate]

How can I give space " " as input in shell script ? Ex : echo " Enter date for grep... Ex: Oct 6 [***Double space is important for single date***] (or) Oct 12 " read mdate echo $mdate I get the ...
4
votes
2answers
5k views

How does the -d option to bash read work?

I have a bash script where I'm trying to assign a heredoc string to a variable using read, and it only works if I use read with the -d '' option, I.e. read -d '' <variable> script block #!/...
4
votes
1answer
9k views

How to use user input as a while loop condition

I can do this in bash: while read -n1 -r -p "choose [y]es|[n]o" do if [[ $REPLY == q ]]; then break; else #whatever fi done which works but seems a bit redundant, can ...
4
votes
1answer
2k views

read timeout parameter (-t) not working?

I'm not sure how to explain the problem in general, so I'll just use this example: #!/bin/bash cleanup() { rm "$myfifo" rm "$mylock" kill '$(jobs -p)' } writer() { for i in $(seq 0 ...
4
votes
3answers
2k views

bash: Prompting for user input while reading file

I'm working on a bash script that parses a tab separated file. If the file contains the word "prompt" the script should ask the user to enter a value. It appears that while reading the file, the "...
4
votes
1answer
747 views

Problem with getting interactive input in while read loop

I was doing something like the following minimal example: #! /bin/sh # $1 = file containing list of files to sync # $2 = source dir ; $3 = target dir cat "$1" | while read f do cp -i "$2"/"$f" "...
4
votes
2answers
623 views

read file line by line and remember the last position in file

I want to grep some line from a log file with an input from another file. I'm using this small command to do that: while read line; do grep "$line" service.log; done < input_strings.txt > ...
4
votes
2answers
455 views

Why bash “read -t 0” does not see input?

My script (should) acts differently, depending on the presence of the data in the input stream. So I can invoke it like this: $ my-script.sh or: $ my-script.sh <<-MARK Data comes... .....
4
votes
2answers
4k views

Read non-bash variables from a file into bash script

My bash script needs to read the variables from a text file that contains hundreds of variables, most of which conform to standard bash variable syntax (e.g., VAR1=VALUE1). However, a few lines in ...
4
votes
1answer
895 views

Custom tab-completion in bash's `read`

Is there a way to "import" a list of words into bash's read builtin to have custom tab-completion? I've been writing a wrapper script for suckless' minimalist irc client "ii", and I don't have a way ...
3
votes
4answers
87k views

Read a file line by line and if condition is met continue reading till next condition [closed]

I have a file foo.txt test qwe asd xca asdfarrf sxcad asdfa sdca dac dacqa ea sdcv asgfa sdcv ewq qwe a df fa vas fg fasdf eqw qwe aefawasd adfae asdfwe asdf era fbn tsgnjd nuydid hyhnydf gby asfga ...
3
votes
3answers
354 views

How can I split a string to letters with IFS and read at no space/null, space and at a character -?

I have a string -w o rd. I need to split it to w o r d or to an array for 'w' 'o' 'r' 'd' it doesn't really matter. I have tried the following IFS='\0- ' read -a string <<< "-w o rd" ...
3
votes
3answers
720 views

In what shared library is read()?

I want to know where the system call read() is. Which shared library is linked when I call read()?
3
votes
3answers
2k views

read command in zsh throws error

In zsh, running the command read -p 'erasing all directories (y/n) ?' ans, throws the error, read: -p: no coprocess But in bash, it prints a prompt. How do I do this in zsh?
3
votes
2answers
3k views

How to read commands from file?

I have some php scripts which I run in sequential order like: php index.php import file1 --offline && php index.php import file2 --deleteUnused && php index.php import file3 Now I ...