You are not logged in.

#1 2025-08-20 09:14:36

iwuvkittens
Member
Registered: 2025-05-30
Posts: 87

can't read file

#!/bin/sh -eu

printf "e?: "
read -r e

export e
sh -c "[ -z "${e}" ] || printf "${e}" > test"

so i have this small script for testing purposes, if input is given when asked for e then it puts that in "test", but i cannot read this file like it's empty for some reason even though if i open it up in a text editor it can be read and i see the input that was thrown in that file

but this works, and i can read the file:

#!/bin/sh -eu

printf "e?: "
read -r e

export e
sh -c "[ -z "${e}" ] || printf \"%b\n\" "${e}" > test"

gnu/linux/tux

Offline

#2 2025-08-20 09:27:22

Scimmia
Fellow
Registered: 2012-09-01
Posts: 13,084

Re: can't read file

So whatever method you're using to read the file requires a newline at the end. The first example doesn't have that as it just passes the variable as the pattern, which isn't good practice.

Online

#3 2025-08-20 09:33:22

iwuvkittens
Member
Registered: 2025-05-30
Posts: 87

Re: can't read file

Scimmia wrote:

So whatever method you're using to read the file requires a newline at the end. The first example doesn't have that as it just passes the variable as the pattern, which isn't good practice.

ty for the response, would you know why it would require a newline at eof. shellcheck also said what you said about bad practice


gnu/linux/tux

Offline

#4 2025-08-20 12:34:04

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,407
Website

Re: can't read file

How exactly are you trying to "read the file" that fails?  You say you can open it in an editor and it's fine.  That is one way of reading a file.  So what *other* method of reading it that produces unexpected results?  And what exactly are those results (i.e., "like it's empty" is rather vague).

Or in other words, when your second post asks why it would require a newline at the end of the file in order to properly read it, it is quite relevant to know exactly what it is.

Last edited by Trilby (2025-08-20 12:35:35)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#5 2025-08-20 12:46:46

iwuvkittens
Member
Registered: 2025-05-30
Posts: 87

Re: can't read file

Trilby wrote:

How exactly are you trying to "read the file" that fails?  You say you can open it in an editor and it's fine.  That is one way of reading a file.  So what *other* method of reading it that produces unexpected results?  And what exactly are those results (i.e., "like it's empty" is rather vague).

Or in other words, when your second post asks why it would require a newline at the end of the file in order to properly read it, it is quite relevant to know exactly what it is.

oh srry, its gnu cat

i execute the script and then provide input to put in e and then it puts it in the file and then i execute cat test


gnu/linux/tux

Offline

#6 2025-08-20 13:46:25

seth
Member
From: Don't DM me only for attention
Registered: 2012-09-03
Posts: 69,068

Re: can't read file

Can you

cat test <(echo)

This frankly sounds more like a problem w/ your shell prompt, you're cat'ing a file that ends w/o a newline what should™ look like

◉ cat test
f%

but since there's no newline you'll get it overridden by the next prompt (or shows up at it's head and you don't see it)

Try cat from

env -i bash --norc
bash-5.3$ cat test
foobash-5.3$

nb. the "foo" at the beginning of the next line

Edit: what exactly is the problem you're trying to address?
https://en.wikipedia.org/wiki/XY_problem

Last edited by seth (2025-08-20 13:46:58)

Offline

#7 2025-08-20 14:04:54

iwuvkittens
Member
Registered: 2025-05-30
Posts: 87

Re: can't read file

there is no problem i already solved it in the op, i was just curious on why it was happening i use zsh so i am not going to see the actual content since there is no newline

KzXd.png

Last edited by iwuvkittens (2025-08-20 14:10:18)


gnu/linux/tux

Offline

#8 2025-08-20 18:26:16

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,407
Website

Re: can't read file

iwuvkittens wrote:

there is no problem i already solved it in the op

It might have been nice if you let the rest of the world know this.

Threads like this is why I very sparingly even bother trying to offer help on these forums anymore.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#9 2025-08-20 18:43:16

iwuvkittens
Member
Registered: 2025-05-30
Posts: 87

Re: can't read file

apologiuze for any confusion


gnu/linux/tux

Offline

#10 2025-08-20 20:25:59

seth
Member
From: Don't DM me only for attention
Registered: 2012-09-03
Posts: 69,068

Re: can't read file

Fwwi, the thread had a purpose in the inquiry of "why", so not a waste of time.
But I still suspect an ongoing xy-situation, because reading input into a variable, writing that into a file an cat'ing that into stdout is sure a nice exercise, but completely pointless.
So the question is/remains whether there ever has been a problem beyond the question of "why does my test debugging did weird".

Offline

#11 2025-08-20 20:38:27

iwuvkittens
Member
Registered: 2025-05-30
Posts: 87

Re: can't read file

nope i was just testing there was no additional problem but i was testing

#!/bin/sh -eu

printf "%s" "hostname: "
read -r hostname

export hostname
arch-chroot /mnt <<EOF
#hostnamectl hostname "${hostname}"
[ -z "${hostname}" ] || printf "%s" "${hostname}" > /etc/hostname
EOF

Last edited by iwuvkittens (2025-08-20 20:39:37)


gnu/linux/tux

Offline

#12 2025-08-21 00:03:42

dimich
Member
From: Kharkiv, Ukraine
Registered: 2009-11-03
Posts: 414

Re: can't read file

iwuvkittens wrote:

[ -z "${hostname}" ] || printf "%s" "${hostname}" > /etc/hostname

man 5 hostname:

The file should contain a single newline-terminated hostname string.

Offline

#13 2025-08-21 10:48:17

iwuvkittens
Member
Registered: 2025-05-30
Posts: 87

Re: can't read file

ty for showing me that i will improve it now


gnu/linux/tux

Offline

#14 2025-08-22 08:50:37

solskog
Member
Registered: 2020-09-05
Posts: 462

Offline

#15 2025-08-22 15:18:27

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,520

Re: can't read file

@solskog, yeah you can't copy the address from the bar...
I think this is the page you meant to share

https://pubs.opengroup.org/onlinepubs/9 … /echo.html
See:APPLICATION USAGE & RATIONALE

Offline

#16 2025-08-22 15:23:39

iwuvkittens
Member
Registered: 2025-05-30
Posts: 87

Re: can't read file

no i will not use echo i dont like it i like printf better


gnu/linux/tux

Offline

#17 2025-08-22 16:38:27

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,520

Re: can't read file

No one is telling you to use echo, but, for the sake of an argument, he's right that echo is shorter tongue
Oh and nice thing about echo; it always sends a newline.

I agree, 'printf' is the better choice because you'd have full control over the output
Some devs even say that echo shouldn't be used anymore and printf should always be used nowadays
echo is still fully supported!

Offline

Board footer

Powered by FluxBB