You are not logged in.
Pages: 1
#!/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
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.
Offline
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
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
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
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
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
Last edited by iwuvkittens (2025-08-20 14:10:18)
gnu/linux/tux
Offline
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
apologiuze for any confusion
gnu/linux/tux
Offline
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
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
[ -z "${hostname}" ] || printf "%s" "${hostname}" > /etc/hostname
The file should contain a single newline-terminated hostname string.
Offline
ty for showing me that i will improve it now
gnu/linux/tux
Offline
It is not possible to use echo portably across all POSIX systems unless escape sequences are omitted, and the first argument does not consist of a '-' followed by one or more characters from the set {'e', 'E', 'n'}.
I’d use echo "$hostname" rather than printf "%s\n" "$hostname" since it’s shorter to type and still portable.
Offline
@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
no i will not use echo i dont like it i like printf better
gnu/linux/tux
Offline
No one is telling you to use echo, but, for the sake of an argument, he's right that echo is shorter
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
Pages: 1