当前位置:网站首页>Execute sh script to prompt "[[: not found" solution. The difference between Bash and sh

Execute sh script to prompt "[[: not found" solution. The difference between Bash and sh

2022-06-12 05:43:00 Amos-Chen

background

Don't want to listen to me , You can jump straight to the end .

Bring your notebook when you buy it windows10, Then because of curiosity , It was tossed with ubuntu( Originally, I wanted to support domestic production deepin, However, there are too many problems ). The root of all evil begins ,ubuntu Many common software cannot be used directly apt install , After installing wechat , After using wechat, you will automatically log out , Unable to unload . But the problem is : Not dpkg/apt/snap Way to install , I don't know how to uninstall , So look for it wechat keyword , Find one wechat.sh.

solve

View the script

part help The code is as follows

help(){
  echo "wechat [-h] [-i] [-f] [-c] [--start|start] [--remove] [--instance]"
  echo "  -h, --help            Show help"
  echo "  -i, --install         Install this script to system"
  echo "  -f, --force           Force install or reinstall"
  echo "  -c, --clean           Clean all wechat container"
  echo "      --start           Start wechat"
  echo "      --update          Update script"
  echo "      --remove          Remove this script"
  echo "      --instance        Create a instance wechat container, you can create more then one using this option"
  return 0
}

Execute the script

./wechat.sh --remove

No reaction ... Looked at the remove Function code , Should be able to wechat.sh The file was deleted , But the files are still there , It can be concluded that the execution was unsuccessful

remove(){
  [ -e ~/.local/bin/wechat.sh ] && rm -f ~/.local/bin/wechat.sh\
  && echo "remove ~/.local/bin/wechat.sh"

  [ -e ~/.local/bin/wechat ] && rm -f ~/.local/bin/wechat

  [ -e ~/.local/share/icons/hicolor/256x256/apps/WINE_WECHAT.png ] \
  && rm -f ~/.local/share/icons/hicolor/256x256/apps/WINE_WECHAT.png\
  && echo "remove ~/.local/share/icons/hicolor/256x256/apps/WINE_WECHAT.png"


  [ -e /home/$(whoami)/.local/share/applications/Wechat.desktop ] \
  && rm -f /home/$(whoami)/.local/share/applications/Wechat.desktop\
  && echo "remove ~/.local/share/applications/Wechat.desktop"

  return 0
}

Debug script

sh -x ./wechat.sh --remove

The tips are shown in the figure

 Insert picture description here
The first 148 The line code is as follows

 while [[ $# > 0 ]];do

Problem solving

Modify the execution method
before:

$ sh wechat.sh

after:

$ bash wechat.sh

Be careful :
see .sh Be sure to look at the first line of the script
wechat.sh The first line of :

#!/usr/bin/env bash

Distinguish between two situations
#!/bin/bash
Or is it
#!/bin/sh

Expand bash vs sh

What is? sh

sh( or Shell Command language ) By POSIX A programming language described by a standard . It has many implementations (ksh88, dash,…).bash It can also be thought of as sh The implementation of the ( See below ).
because sh Is the specification , Not to achieve ,/bin/sh In most cases POSIX Symbolic connection actually implemented on the system (or Hard links ).

What is? bash

bash Is compatible sh An implementation of ( Although it was regarded as POSIX standard ), But over time , It needs more extensions . Some of these extensions will change the effective POSIX shell The behavior of the script , therefore bash It is not effective in itself POSIX shell. And then it's POSIX shell Dialects of language .
bash It can be executed --posix Switch , Make it more compatible POSIX, Also try calling sh To imitate POSIX.

sh=bash?

For a long time , In most GNU/Linux On the system ,/bin/sh Is directed /bin/bash. result , The difference between the two can almost be ignored . But this situation has recently begun to change .

stay /bin/sh Don't point to /bin/bash( In some cases it doesn't even exist /bin/bash) In the system , Some common examples are :
1. modern debian and ubuntu On the system ,sh The default is dash The symbolic link
2.Busybox, It's usually in Linux When the system boots, it acts as initramfs Part of running . It has been used. ash shell Realization .
3.BSDs, And usually all non linux System .OpenBSD Use pdksh,Korn shell The offspring of .FreeBSD Of sh It's primitive UNIX Bourne shell The offspring of .Solaris It has its own sh But for a long time, it has not been with POSIX Compatible , It's a kind of Heirloom The project provides an open source implementation .

How to find /bin/sh The point on our system

/bin/sh The complexity of : It can be a symbolic link or a hard link .
If it's a symbolic link , It can be done by :

% file -h /bin/sh
/bin/sh: symbolic link to bash

If it's a hard link , You can try :

% find -L /bin -samefile /bin/sh
/bin/sh
/bin/bash

actually -L Signs include both symbolic links and hard links , But the disadvantage of this method is that it is not portable ,POSIX Unwanted find To support -samefile Options , Even though GNU find and FreeBSD find All support it .

Shebang

In the field of computing ,Shebang( Also known as Hashbang) Is a character sequence composed of pound sign and exclamation mark #!, It appears in the first two characters of the first line of the text file .

Final , By writing in the first line of the script Shebang To decide to use sh still bash:
For example, use sh

#!/bin/sh

Or use /bin/bash If available ( If unavailable, it will fail with an error message )

#!/bin/bash

Of course, you can choose another implementation , such as

#!/bin/dash

Which one to use

In my script , Tend to use sh There are the following reasons :

  1. It's standard
  2. Very simple and easy to learn
  3. Portable and cross POSIX System , Even if it doesn't happen bash, There needs to be sh

Use bash There are also advantages . Its features make programming more convenient , And it is similar to programming in other modern programming languages . These include things like scoped local variables and arrays . pure sh Is a very simple programming language .

Reference resources

string-comparison-in-bash-not-found
difference-between-sh-and-bash

原网站

版权声明
本文为[Amos-Chen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010614149940.html