document that read built-in can't return zero-length string in the middle of input

2024-01-10 Thread ilya Basin
Dear.
I needed to read 16 bytes from a binary file and tried to replace a hexdump 
call with read built-in. I expected that with "-N1" if a NUL character is 
encountered bash would assign an empty string, however there's no indication 
that a NUL character was there and it simply assigns the next non-NUL character 
to the variable.
Example:

$ printf 'a\0c' | { LC_ALL=C; read -r -N1 a; read -r -N1 b; read -r -N1 c; 
echo "a=$a"; echo "b=$b"; echo "c=$c"; }

Expected:
a=a
b=
c=c

Actual:
a=a
b=c
c=

That's questionable, but fine with me. Yet I couldn't find this in the man 
page. Can we document it?



substitution read all from fd silently fails: $(0)

2015-07-01 Thread Ilya Basin
Hi list.

Want to read whole stdin into variable.
Don't want to spawn new processes (cat).
Don't want to reopen existing fd 0

First thing I tried: $(0)
It silently returns an empty string.

From bash manual:
The command substitution $(cat file) can be replaced by the equivalent but 
faster $( file).


reopening /dev/stdin mostly works, but easily broken by sudo:

# same user. works
[root@okdistr ~]# echo aaa | bash -c 'echo $(/dev/stdin)'
aaa

# different user. fail
[root@okdistr ~]# echo aaa | sudo -u nobody bash -c 'echo $(/dev/stdin)'
bash: /dev/stdin: Permission denied

# spawn new process. not want
[root@okdistr ~]# echo aaa | sudo -u nobody bash -c 'echo $(cat)'
aaa

# try to read from fd: silently fails
[root@okdistr ~]# echo aaa | sudo -u nobody bash -c 'echo $(0)'

# works, but too complex
[root@okdistr ~]# echo aaa | sudo -u nobody bash -c 'a=; while true; do 
rc=0; read -N1024 b || rc=$?; a=$a$b; [ $rc = 0 ] || break; done; echo $a'
aaa

[root@okdistr ~]#





Re[2]: ~/.bashrc not sourced for ssh commands on Archlinux

2013-04-24 Thread Ilya Basin
   

On Wed, Apr 24, 2013 at 7:31 AM, Ilya Basin [1]basini...@gmail.com
wrote:

Hi.

$ cat ~/.bashrc

#

# ~/.bashrc

#

echo Im in .bashrc 2

RHEL 6.0, bash 4.1.2

$ ssh localhost 'ps -f $$  true'

Im in .bashrc

UIDPID  PPID  C STIME TTY  STAT   TIME CMD

git  22295 22294  0 08:29 ?Ss 0:00 bash -c ps -f $$
 true

Archlinux, bash 4.2.42

$ ssh localhost 'ps -f $$  true'

UIDPID  PPID  C STIME TTY  STAT   TIME CMD

il   26539 26538  0 08:26 ?Ss 0:00 bash -c ps -f $$
 true

What can couse this? Manpage says: Bash attempts to determine when it

is being run with its standard input connected to a network

connection. Is the detection broken?

With newer sshd stdin is not connected to a socket.

There is a compile time option to cause bash to check for the
SSH_CLIENT[2] environment variables but it seems arch doesn't enable it
for its build.

   Socket? How could it ever be connected to a socket, if the data has to
   be decrypted first?

   --

References

   1. mailto:basini...@gmail.com


~/.bashrc not sourced for ssh commands on Archlinux

2013-04-23 Thread Ilya Basin
Hi.

$ cat ~/.bashrc
#
# ~/.bashrc
#
echo Im in .bashrc 2

RHEL 6.0, bash 4.1.2
$ ssh localhost 'ps -f $$  true'
Im in .bashrc
UIDPID  PPID  C STIME TTY  STAT   TIME CMD
git  22295 22294  0 08:29 ?Ss 0:00 bash -c ps -f $$  true

Archlinux, bash 4.2.42
$ ssh localhost 'ps -f $$  true'
UIDPID  PPID  C STIME TTY  STAT   TIME CMD
il   26539 26538  0 08:26 ?Ss 0:00 bash -c ps -f $$  true


What can couse this? Manpage says: Bash attempts to determine when it
is being run with its standard input connected to a network
connection. Is the detection broken?




prevent ignore SIGINT for asynchronous commands without enabling job control

2013-04-10 Thread Ilya Basin
Hi.
I have a script that creates some background tasks.
I want the whole tree to be killed by Ctrl-C.

There's a requirement that the script process and its children must
belong to the same process group. This is why I can't enable job
control.

I don't want to use 'trap', because it adds complexity.

Example:

#!/bin/bash
sleep 90 
wait $!

After Ctrl-C sleep 90 stays.

Using trap:

#!/bin/bash

trap 'trap - INT; kill -TERM $noeof_pid; kill -INT $$' INT

sleep 90 
wait $!





bug or feature? Ctrl+C sometimes can't interrupt a loop with multiple enclosed pipes

2010-06-28 Thread Ilya Basin
Configuration Information [Automatically generated, do not change]:
Machine: i686
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='i686' 
-DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='i686-pc-linux-gnu' 
-DCONF_VENDOR='pc' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash' -DSHELL 
-DHAVE_CONFIG_H   -I.  -I. -I./include -I./lib   -march=i686 -mtune=generic -O2 
-pipe
uname output: Linux IL 2.6.33-ARCH #1 SMP PREEMPT Thu May 13 12:06:25 CEST 2010 
i686 Intel(R) Core(TM)2 Duo CPU E6550 @ 2.33GHz GenuineIntel GNU/Linux
Machine Type: i686-pc-linux-gnu

Bash Version: 4.1
Patch Level: 7
Release Status: release

Description:
Sample script included.
When I press Ctrl+C, only the inside loop is interrupted. The outside loop 
keeps going.
In zsh and in old plain sh Ctrl+C works.

Repeat-By:
run this sample script and try to interrupt it with Ctrl+C:
#
  { echo 1; echo 2; echo 3; } | while read a; do
echo $a
{ echo 1; echo 2; echo 3; } | while read b; do
  echo $a-$b
  sleep 1
done
  done
#