Bug 37272 - Определение переменной для конкретной функции перезаписывает значение глобально
Summary: Определение переменной для конкретной функции перезаписывает значение глобально
Status: NEW
Alias: None
Product: Sisyphus
Classification: Development
Component: sh4 (show other bugs)
Version: unstable
Hardware: all Linux
: P3 normal
Assignee: placeholder@altlinux.org
QA Contact: qa-sisyphus
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-09-28 12:50 MSK by Vitaly Lipatov
Modified: 2023-11-11 18:35 MSK (History)
7 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Vitaly Lipatov 2019-09-28 12:50:19 MSK
По неожидаемой таинственной причине переопределение переменной при вызове функции из shell переопределяет переменную глобально, если использовать sh, а не bash в качестве интерпретатора.

$ ssh builder-p8 sh ./z.sh
ALTLinux/p8
 $ rpm -qf /bin/sh
sh-3.2.57-alt1
TESTIK 1: ORIGINAL
TESTIK 2: ORIGINAL
TESTIK 3: REPLACED after func
TESTIK 4: REPLACED after func

$ ssh builder-p8 bash ./z.sh
ALTLinux/p8
 $ rpm -qf /bin/bash
bash-3.2.57-alt1
TESTIK 1: ORIGINAL
TESTIK 2: ORIGINAL
TESTIK 3: REPLACED after func
TESTIK 4: ORIGINAL

$ ssh builder-p9 sh ./z.sh
ALTLinux/p8
 $ rpm -qf /bin/sh
sh-4.4.23-alt1.noarch
Note: /bin/sh is link to /bin/sh4
 $ rpm -qf /bin/sh4
sh4-4.4.23-alt1.x86_64
TESTIK 1: ORIGINAL
TESTIK 2: ORIGINAL
TESTIK 3: REPLACED after func
TESTIK 4: REPLACED after func

$ ssh builder-p9 bash ./z.sh
ALTLinux/p8
 $ rpm -qf /bin/bash
bash-4.4.23-alt1.noarch
Note: /bin/bash is link to /bin/bash4
 $ rpm -qf /bin/bash4
bash4-4.4.23-alt1.x86_64
TESTIK 1: ORIGINAL
TESTIK 2: ORIGINAL
TESTIK 3: REPLACED after func
TESTIK 4: ORIGINAL

$ ssh builder64 sh ./z.sh
ALTLinux/Sisyphus
 $ rpm -qf /bin/sh
sh-4.4.23-alt1.noarch
Note: /bin/sh is link to /bin/sh4
 $ rpm -qf /bin/sh4
sh4-4.4.23-alt1.x86_64
TESTIK 1: ORIGINAL
TESTIK 2: ORIGINAL
TESTIK 3: REPLACED after func
TESTIK 4: REPLACED after func

$ ssh builder64 bash ./z.sh
ALTLinux/Sisyphus
 $ rpm -qf /bin/bash
bash-4.4.23-alt1.noarch
Note: /bin/bash is link to /bin/bash4
 $ rpm -qf /bin/bash4
bash4-4.4.23-alt1.x86_64
TESTIK 1: ORIGINAL
TESTIK 2: ORIGINAL
TESTIK 3: REPLACED after func
TESTIK 4: ORIGINAL


$ cat z.sh
#!/bin/sh

test_func()
{
    echo "TESTIK $1: $testik"
}

distr_info
epmqf /bin/sh
epmqf /bin/bash

testik="ORIGINAL"
test_func 1
testik='REPLACED after command' /bin/true
test_func 2
testik='REPLACED after func' test_func 3
test_func 4
x
Comment 1 Dmitry V. Levin 2019-09-28 14:58:37 MSK
$ sh -c 'foo(){ echo "a=$a"; }; echo $BASH_VERSION; a=1; foo; a=2 foo; foo'
4.4.23(1)-release
a=1
a=2
a=2

$ bash -c 'foo(){ echo "a=$a"; }; echo $BASH_VERSION; a=1; foo; a=2 foo; foo'
4.4.23(1)-release
a=1
a=2
a=1
Comment 2 Vitaly Lipatov 2019-10-19 18:01:22 MSK
А можно узнать, какие планы по исправлению?
И что делать? Искать проблему самому, переходить на использование bash?
Comment 3 Vitaly Lipatov 2019-11-05 21:30:27 MSK
По моим сведениям, проблему вызывает использование
--disable-restricted
при сборке sh
Comment 4 Vitaly Lipatov 2019-11-05 22:17:23 MSK
(В ответ на комментарий №3)
> По моим сведениям, проблему вызывает использование
> --disable-restricted
> при сборке sh
Прошу прощения, проблему вызывает отличие в названии файла:

[lav@builder64 build-sh]$ md5sum sh4 bash
222e0437b72384f06b5182dec89615ac  sh4
222e0437b72384f06b5182dec89615ac  bash
[lav@builder64 build-sh]$ ./bash -c 'foo(){ echo "a=$a"; }; echo $BASH_VERSION; a=1; foo; a=2 foo; foo'
4.4.23(1)-release
a=1
a=2
a=1
[lav@builder64 build-sh]$ ./sh4 -c 'foo(){ echo "a=$a"; }; echo $BASH_VERSION; a=1; foo; a=2 foo; foo'
4.4.23(1)-release
a=1
a=2
a=2
Comment 5 Ivan A. Melnikov 2019-11-06 11:39:26 MSK
Хотелось бы отметить следующее:

$ sh3 -c 'foo(){ echo "a=$a"; }; echo $BASH_VERSION; a=1; foo; a=2 foo; foo'
3.2.57(1)-release
a=1
a=2
a=2
$ bash3 -c 'foo(){ echo "a=$a"; }; echo $BASH_VERSION; a=1; foo; a=2 foo; foo'
3.2.57(1)-release
a=1
a=2
a=1
$ dash -c 'foo(){ echo "a=$a"; }; echo $BASH_VERSION; a=1; foo; a=2 foo; foo'

a=1
a=2
a=2
$ rpm -qf `which dash`
ash-0.5.8-alt1.2e5842258.x86_64
Comment 6 Vitaly Lipatov 2023-11-11 18:35:37 MSK
(Ответ для Dmitry V. Levin на комментарий #1)
> $ sh -c 'foo(){ echo "a=$a"; }; echo $BASH_VERSION; a=1; foo; a=2 foo; foo'
> 4.4.23(1)-release
> a=1
> a=2
> a=2
> 
> $ bash -c 'foo(){ echo "a=$a"; }; echo $BASH_VERSION; a=1; foo; a=2 foo; foo'
> 4.4.23(1)-release
> a=1
> a=2
> a=1

Для bash5 разницы больше нет:

[lav@builder64 beeheave]$ sh -c 'foo(){ echo "a=$a"; }; echo $BASH_VERSION; a=1; foo; a=2 foo; foo'
5.2.15(1)-release
a=1
a=2
a=1
[lav@builder64 beeheave]$ bash -c 'foo(){ echo "a=$a"; }; echo $BASH_VERSION; a=1; foo; a=2 foo; foo'
5.2.15(1)-release
a=1
a=2
a=1