bash returns wrong variable value/call more proccesses?

Bug #1958060 reported by Dark Archangel
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
bash (Ubuntu)
Expired
Undecided
Unassigned

Bug Description

Simple function gives different results when interpretator set in first string of script
CHECKPROC()
{
i=$(ps aux | grep -c $0)
ps aux | grep -c $0
echo variable is $i
sleep 10
}
CHECKPROC

Expected
In any conditions it will return same values, then will increments when other copies of script will be found in proccesses list.

Happened.
We set interpretator with #!/bin/bash or #!/bin/sh in first string of script and runs multiple copies of script from one terminal with & after scriptname. It's returns "1" and "variable is 1" always.
Without interpretator setup, when string #!/bin/bash or #!/bin/sh deleted.
Run multiple copies scriptname.sh&
On every iteration it returns right first value and "variable is $value+1"

$scriptname.sh&
2
variable is 3
$scriptname.sh&
3
variable is 4
...and so on.

Without counting in first case it returns only string with grep, in second case -- one more string with different PID and script name when called from i=$(...) or i=`...`

Ubuntu 14.04 x32, bash 4.3.11(1)-release
Same results from other man on
GNU bash, version 5.1.16(1)-release (aarch64-apple-darwin21.1.0)
GNU bash, version 4.4.20(1)-release (x86_64-pc-linux-gnu)

lsb_release -rd
Description: Ubuntu 14.04.6 LTS
Release: 14.04

apt-cache policy bash
bash:
  Установлен: 4.3-7ubuntu1.7
  Кандидат: 4.3-7ubuntu1.7
  Таблица версий:
     4.3-7ubuntu1.8+esm1 0
       -32768 https://esm.ubuntu.com/ubuntu/ trusty-infra-security/main i386 Packages
 *** 4.3-7ubuntu1.7 0
        500 http://ru.archive.ubuntu.com/ubuntu/ trusty-updates/main i386 Packages
        500 http://security.ubuntu.com/ubuntu/ trusty-security/main i386 Packages
        100 /var/lib/dpkg/status
     4.3-6ubuntu1 0
        500 http://ru.archive.ubuntu.com/ubuntu/ trusty/main i386 Packages

Tags: bot-comment
Revision history for this message
Ubuntu Foundations Team Bug Bot (crichton) wrote :

Thank you for taking the time to report this bug and helping to make Ubuntu better. It seems that your bug report is not filed about a specific source package though, rather it is just filed against Ubuntu in general. It is important that bug reports be filed about source packages so that people interested in the package can find the bugs about it. You can find some hints about determining what package your bug might be about at https://wiki.ubuntu.com/Bugs/FindRightPackage. You might also ask for help in the #ubuntu-bugs irc channel on Freenode.

To change the source package that this bug is filed about visit https://bugs.launchpad.net/ubuntu/+bug/1958060/+editstatus and add the package name in the text box next to the word Package.

[This is an automated message. I apologize if it reached you inappropriately; please just reply to this message indicating so.]

tags: added: bot-comment
Revision history for this message
Dark Archangel (yuri-xarit) wrote :

up.
String without shebang returns 0 for pgrep -c $0 or 1 for ps aux | grep -c $0.
Solved with help of user Valrust from forum.ubuntu.ru, it's standart behavior of the system.
In that case new copy of script runs as "bash" without agruments and try to find /address/name.sh in ps aux returns nothing, but pgrep -c bash returns more and more values depends from run copies.

Paul White (paulw2u)
affects: ubuntu → bash (Ubuntu)
Revision history for this message
Brian Murray (brian-murray) wrote :

Trusty is no longer a supported release of Ubuntu. Have you tested this with a supported release of Ubuntu? If so what version of bash were you using?

Changed in bash (Ubuntu):
status: New → Incomplete
Revision history for this message
Dark Archangel (yuri-xarit) wrote :

Brian Murray (brian-murray)
It's tested other man on ubuntu 20.04 x64 and apple mac pro with m1 pro CPU.
GNU bash, version 5.1.16(1)-release (aarch64-apple-darwin21.1.0)
GNU bash, version 4.4.20(1)-release (x86_64-pc-linux-gnu)
I will test it from lubuntu 20.04 x64 livecd and will add new results.

Revision history for this message
Dark Archangel (yuri-xarit) wrote (last edit ):

...
Tested on Lubuntu 20.04, results are the same.
https://sun9-21.userapi.com/impg/epKIlh1A6x-RtHgZ5EUERBK59vFYIAJcg_c3YQ/7iW2z_L1Ajc.jpg?size=1063x500&quality=95&sign=cae5f41c4a9a41ffc7d0ad65bde94db8&type=album

apt-cache policy bash
bash:
  Installed: 5.0-6ubuntu1.1
  Candidate: 5.0-6ubuntu1.1
  Version table:
 *** 5.0-6ubuntu1.1 500
        500 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages
        100 /var/lib/dpkg/status
     5.0-6ubuntu1 500
        500 http://archive.ubuntu.com/ubuntu focal/main amd64 Packages

lsb_release -rd
Description: Ubuntu 20.04.2 LTS
Release: 20.04

Revision history for this message
Launchpad Janitor (janitor) wrote :

[Expired for bash (Ubuntu) because there has been no activity for 60 days.]

Changed in bash (Ubuntu):
status: Incomplete → Expired
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.