Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [read]

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

0
votes
2answers
29 views

Bash - here doc read input value from another user

I have a here document inside a bash script. I want to read a value from it like that : su myUser<<SESSION set -x echo -n "Enter your name and press [ENTER]: " read name echo "the name is $...
0
votes
0answers
28 views

append to a string in loop not working

I have a ksh script, where I am trying to read from file in a loop and append certain values to each line in the file. It is not working. Below is the code: Code snippet: input="$...
0
votes
1answer
38 views

Process exits unexpectedly when sending to a named pipe

I have two processes and one named pipe. One process is actively reading from the pipe, and one process is sending data to the pipe continously. The command that sends to the pipe is the following :...
0
votes
2answers
56 views

How to read stderr from the process running in the background?

I wanna send the daemon into the background and only continue my script execution when the daemon outputs the particular line to the stderr, something among the lines: # Fictional daemon { for x ...
0
votes
2answers
110 views

Interpret variables from read in string with shell script [duplicate]

I have a text file which is usually filled with multiple lines which I want to "print" with a while loop. The text inside this file contains variables - my problem is, that these variables are not ...
2
votes
1answer
52 views

Using read command to read file by line in Bash doesn't work

I have a text.txt file like this line1 line2 line3 I want to write a script that loops over each line and echo out modified line1 modified line2 modified line3 This is the script which is a very ...
16
votes
1answer
973 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 ...
8
votes
4answers
588 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. ...
2
votes
1answer
44 views

Remove duplicate adjacent lines from file

Say we have file like so: foo1 bar foo2 foo2 bar bar bar foo3 I want it reduced to: foo1 bar foo2 bar foo3 basically removing duplicates only if they are adjacent...I started writing a bash ...
1
vote
1answer
23 views

read -s gives error via script

I am trying to read a password from user, i used -s silent flag but read -s is not working from the script but it works if i do it manually from terminal. error details project.sh: 3: read: Illegal ...
0
votes
0answers
33 views

Alternative to `while read line` for pipe

I have this: export var=1 kbc get pods -o=json | jq -r '.items[].metadata.name' | while read line; do echo "$line"; done; kbc get pods -o=json | jq -r '.items[].metadata.name' | xargs echo var=$...
2
votes
1answer
108 views

Why isn't it possible to read from `stdin` with `read` when piping a script to bash?

I'm not looking for work-arounds or solutions for the issue. I'm fine with it not working like that in bash. I just don't understand why it doesn't work. I'm looking for an in-depth answer why the ...
0
votes
1answer
136 views

Read command in bash script not executing as a read command and outputting text straight to command line [closed]

I have a bash script that I wrote to automate some commands, and one of the first lines in the script isn't working on the computer that it needs to run on. The code is below #!/bin/bash #some ...
1
vote
1answer
644 views

Split string by space in ZSH

Give this file.txt: first line second line third line This works in bash: while IFS=' ' read -a args; do echo "${args[0]}" done < file.txt To produce first second third That is to say, we ...
0
votes
1answer
103 views

Reset terminal after interrupting `read -r -s`

If I run read -r -s INPUT And then interrupt it with Ctrl-C, then the terminal stays in a state where all the input characters are not shown. How can I restore the terminal after such an incident?
7
votes
1answer
247 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 ...
1
vote
1answer
223 views

Speed up persistent live usb disk operations

I created a persistent Debian 9 live usb. The persistence is configured with / union. An unexpected consequence, although obvious in hindsight, is the system lags on non-cached reads: holmes@...
0
votes
1answer
27 views

set alias to read variable and then use in second command; only works when I execute them manually

I am with CentOS 7 and I want to bind an alias to launch PostgreSQL shell(psql). I defined this alias and append it in /etc/profile.d/alias: alias psql-local="read -p \"PSQL: enter the DB to connect: ...
4
votes
2answers
226 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... .....
0
votes
1answer
123 views

Reading multiple files and operating on stored Arrays

I have 2 text files "${LinkP}" and "${QuestionP}. I want to read these files and store each complete line in the respective array, IFS=$'\r\n' GLOBIGNORE='*' command eval "LinkA=($(cat "${LinkP}"))" ...
3
votes
2answers
124 views

Should the shell read (an script) one character at a time?

While reading an script, the shell will read the script from a file, or from a pipe or possibly, some other source (stdin ?). The input may not be seek able under some corner conditions (no way to ...
2
votes
1answer
489 views

Does Linux kernel check file permissions on inode or open file description?

I wrote this testing code, and find that this program can always read the file successfully even after I canceled the read permission when running getchar(). #include <stdio.h> #include <...
1
vote
1answer
261 views

How can I make `read` read from stdin? [duplicate]

bash builtin command read is said to accept input from stdin, but why does the following not read in anything? $ printf "%s" "a b" | read line $ printf "%s" "$line" $ Thanks.
1
vote
3answers
138 views

Is there a character as `delim` of `read`, so that `read` reads the entire of a file at once?

In bash manual, about read builtin command -d delim The first character of delim is used to terminate the input line, rather than newline. Is it possible to specify a character as delim of read, ...
3
votes
3answers
304 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" ...
0
votes
1answer
25 views

Why does read from variable give blank new variable? [closed]

I am trying to get a folder name from a stored variable string. When I ran the following path="Folder%20Name/Dir/File" read -d "/" folder < <(echo ${path/\%20/ }) echo "$folder" I am getting a ...
0
votes
1answer
128 views

read issues with ls -i

If I write: ls -i '/home/user/Desktop/file' | awk '{print $1}' I get the inode number of the specified file. I want to make a script that gives me back the inode number of a certain file. If I do: ...
0
votes
1answer
46 views

while read loop seems to terminate after the first line, when using two pipes?

#!/usr/bin/env bash set -euo pipefail while read -r line do echo "line via echo:" echo "$line" echo "line via sed:" sed 's/w/f/g' | sed 's/f/zzzzz/g' <<< "$line" done < /dev/...
0
votes
1answer
124 views

Input from readline owerwrites the prompt

I'm trying to create an interactive bash script, where i can call given options from 1-n or just them like commands. It will end up with a simulated prompt and 'read' is used to get the input, ofc. ...
0
votes
1answer
62 views

Is this bash argument substitution?

While studying bash process substitution, I found this: counter=0 while IFS= read -rN1 _; do ((counter++)) done < <(find /etc -printf ' ') echo "$counter files" If I understand correctly,...
1
vote
2answers
194 views

read-verification alternative (two prompts and if-then comparison alternative)

I am trying to create a small script for creating simple, all-default Apache virtual host files (it should be used any time I establish a new web application). This script prompts me for the domain....
2
votes
1answer
59 views

What system call does Evince use to open pdf?

I have tried to hook system calls using linux kernel module. However, when I open a pdf file using Evince, I find no open,read and write is used on this specific file, only lstat is used. Here is ...
1
vote
1answer
208 views

read command with ??? input

Why when reading input with read, and the input is ??? the result is bin src? $ read ??? $ echo $REPLY bin src Running bash on macOS.
6
votes
4answers
3k 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 ...
3
votes
1answer
163 views

BASH question: using read, can I capture a single char OR arrow key (on keyup)

So here's my deal: working in BASH, I have already built out a function which works just fine that accepts an array or any number of parameters, and spits out an interactive menu, navigable by arrows ...
1
vote
1answer
150 views

read(2) blocking behaviour changes when pts is closed resulting in read() returning error: -1 (EIO)

I'm trying to figure out how I can reliably loop a read on a pt master I have. I open the ptmx, grant and unlock it as per usual: * ptmx stuff */ /* get the master (ptmx) */ int32_t masterfd = open("/...
5
votes
2answers
549 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 != "" ]] ...
1
vote
0answers
202 views

read -rsn1 usage in bash script [duplicate]

I have written a script that is to display content in a directory, take user input, and then write the path to a .txt file based on the user input. The code is to wait for user input before displaying ...
0
votes
1answer
261 views

read from stdin works differently in bash and zsh [duplicate]

I'm trying to pipe a command's output to the read builtin of my shell and I get a different behaviour for zsh and bash: $ bash -c 'echo hello | read test; echo $test' $ zsh -c 'echo hello | read ...
1
vote
1answer
55 views

Prevent Linux from writing to HDD

I have 3 HDD's, I want to install Linux Mint on HDD 3 which is currently empty and I don't want Linux to be able to write any data on the other 2 which is where all my Windows files are, is there a ...
2
votes
1answer
198 views

why REPLY variable in read builtin skip white space?

echo " a" | while read; do echo "$REPLY"; done will output ".....a" which contains leading white space. However, echo " a" | while read line; do echo "$line"; done will output "a" with ...
0
votes
2answers
56 views

how does read(3) handle when nbyte < 0?

I couldn't find any behavior defined in the man page for read(3). My guess would be that it either throws an error or it just returns 0. But if someone could confirm any defined behavior that would be ...
0
votes
1answer
72 views

How to read only the last line of a live input stream in a while read loop

I have a very simple bash script that synchronizes data to a USB key as soon as new files appear. It is poorly written but it works, however I want to minimize CPU time so I used sleep but since I use ...
1
vote
3answers
50 views

Treating the input for the read command as a command itself

Let's say I defined this function in the script: fct1() { local msg1=${@} if [[ "${verb}" = 'tru' ]]; then echo "I say $msg1" sleep 1 echo "i repeat" sleep 1 echo "I saaaaaaaaay $msg1" ...
0
votes
1answer
39 views

Why would `read -s` behaves differently than `read` if killed with `ctrl-c`?

Consider this script: #!/bin/bash echo "hi there $(whoami)" [ "`whoami`" = "root" ] || { exec sudo -u root "$0" "$@" echo "this is never called" } read -s -p "enter stuff: " stuff echo "answer:...
1
vote
2answers
186 views

Execute semicolon separated commands passed to the -e flag of st (Simple Terminal)

NOTE: st is the actual name of the terminal emulator in my question - https://st.suckless.org/. I want to create a shortcut that if pressed, pops up st and displays the translation of the word in the ...
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 ...
2
votes
1answer
2k views

How to use multiple variable for input with read command?

I am using GNU bash - version 4.2.10(1). I want to read multiple variables using single read command in shell script. So i tried as below: echo " Enter P N R : " read P N R but it's not working. It ...
1
vote
2answers
35 views

Read list of servers for user?

How can i read list of server entered by user & save it into variable ? Example: Please enter list of server: (user will enter following:) abc def ghi END $echo $variable abc def ghi I want ...
1
vote
4answers
923 views

Treat “\r” as nothing in “while read -r”

I have this line of code that reads a text file line by line. The text file is sometimes generated by a Windows user, sometimes by a Unix user. Therefore, sometimes I see \r\n at the end of the line ...