当前位置:网站首页>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

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 :
- It's standard
- Very simple and easy to learn
- 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
边栏推荐
- [JS knowledge] easily understand JS anti shake and throttling
- Vivado HLS introductory notes
- Towards End-to-End Lane Detection: an Instance SegmentationApproach
- Go 面向接口编程实战
- 38. appearance series
- Introduction to sringmvc
- Beginning is an excellent emlog theme v3.1, which supports emlog Pro
- yolov5
- 59 - I. maximum value of sliding window
- [speech] how to customize ring back tone according to different countries
猜你喜欢

Is the individual industrial and commercial door a legal person enterprise

Chapter 8 - structure

Chapter 7 - pointer learning

Wireshark filter rule

16. 最接近的三数之和

什么是工程预付款

16. Somme des trois plus proches

Nature | 给全球的新冠伤亡算一笔账

Nature | make an account of the new crown casualties in the world

Automated test - dark horse headline test project
随机推荐
Select gb28181, RTSP or RTMP for data push?
Details of FPGA syntax
38. arrangement of strings
59 - II. Maximum value of the queue
37. serialized binary tree
个体工商户是不是法人企业
[go] Viper reads the configuration file in the go project
A solution for PHP to implement image login verification code
[fastapi] use pycharm to configure and use environment variables for fastapi projects
About architecture (in no particular order)
Halcon uses points to fit a plane
The application could not be installed: INSTALL_FAILED_TEST_ONLY
Lldp protocol
SIM卡信号的驱动电流是多少mA,是否是可调节的?
Why should state-owned enterprises go public
43. Number of occurrences of 1 in 1 ~ n integers
[JS knowledge] easily understand JS anti shake and throttling
RTMP streaming +rtmp playback low delay solution in unity environment
[gin] gin framework for golang web development
16. 最接近的三數之和