Stack Exchange Network

Stack Exchange network consists of 174 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 function used to read a line from the standard input, or from a file descriptor.

1
vote
1answer
24 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 ...
-2
votes
0answers
27 views

Are file_operations considered a callback? [closed]

I argued with a friend of mine regarding file_operations and the Kernel's implementation of write/read etc syscalls, and we know that in fact the actual implementation of read, or write, is under a ...
0
votes
1answer
49 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
169 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
25 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
21 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
64 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
34 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
105 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
103 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
60 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
111 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
171 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
23 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
56 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
0answers
31 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
78 views

Input from readline owerwrites the prompt

So 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
61 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
3answers
177 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
44 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
74 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.
4
votes
4answers
1k 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
113 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
61 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("/...
2
votes
2answers
156 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
87 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
107 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
51 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
43 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
45 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
60 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
45 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
0answers
95 views

UDEV Run Script Cannot Read Device Unless Root

I'm using floppy diskettes as ID cards. To accomplish this (on raspi debian jessie) I have created the following udev rule to kick off a script which runs a Java program to verify information written ...
0
votes
1answer
37 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:...
0
votes
2answers
87 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
1k 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
1k 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
389 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 ...
1
vote
2answers
39 views

Can we use read(), write() on a directory in unix/linux?

Can we use read(), write() on a directory just like on any other file in Unix/Linux? I have a confusion here because directories are also considered as files.
3
votes
3answers
1k 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 "...
1
vote
2answers
95 views

Using 'read' for more than one variable

So I'm writing a script to basically run my docker applications from quickly, I've got everything working just fine it does everything I coded it to do. I just have a question about one of my ...
0
votes
1answer
198 views

Why does bash 'read' exit with status 1?

From an answer to "Bash script doesn't see SIGHUP?", I've got the following in my script: while true; do read; done And, usually, this works fine. However, when run from (tl;dr) ... something else, ...
0
votes
1answer
319 views

bash: Reading a full record from a fifo

I wish to send a command to process A, from process B, via a FIFO. The command will be a word or a sentence, but wholly contained on a "\n" terminated line - but could, in general, be a multi-line ...
2
votes
1answer
165 views

sort: read failed: ./folder/: Is a directory [closed]

When I run: du -sh ./*/ I get the following error: sort: read failed: ./folder/: Is a directory How do I fix this? Is there something wrong with sort on my system. I am running x86_64 Linux 4.16....
1
vote
1answer
88 views

Bash script Issue parsing text in line with whitespace characters

I have a list of commands to parse through for an audit, similar to this: 1. -a *policy name=PolicyName -a *policy workflow name=PolicyWorkflow -a *policy action name=PolicyAction -s Server -b ...
0
votes
2answers
862 views

Wait for keyboard input inside a while-read loop

I have a bash script with a construction like this: while read foo bar baz; do echo "Processing $foo $bar $baz" # more code here done < /etc/somefile Inside the loop, I would like the ...
2
votes
0answers
62 views

Linux AHCI/ATA read errors not reported

After a recent OS upgrade (to current Tumbleweed, Linux Kernel 4.16.0 from April 4th), when reading from a faulty hard disk or from damaged/scratched DVDs, I don't get anymore read errors in the ...
0
votes
0answers
20 views

Reading existing screen displayed values

Using BASH, is there a way to capture characters that on currently on the screen? More specifically, I need to display a screen, with what is currently in a variable, and allow the user to cursor ...
2
votes
1answer
68 views

sequence command and test question

Here is a test question that I am stuck with: Which output will the following command sequence produce? echo '1 2 3 4 5 6' | while read a b c; do echo result: $c $b $a; done And the correct answer ...