[[: Not Found error resolution in bash script]

Source: Internet
Author: User
Tags posix

[[: Not Found error resolution--bash in Bash script summary 1

Today, when writing a script, there is a strange problem: use [[: Not found] in the script. It is natural to solve problems when encountering problems.

1. Is the bash version used too low?

Bash--version View bash version information as follows

[Email protected]:~ $bash--version

GNU Bash, version 3.2.39 (1)-release (I486-PC-LINUX-GNU)

Copyright (C) Free Software Foundation, Inc.

In Google Bash Manual, 3.2.39 is not low enough to fully support [[such an extension. It doesn't appear to be a version issue.

2. is in the script [[use error?]

Write test scripts to test. The contents of the Test.sh test script are as follows


[[1]] && echo "successful"

is still "[[: Not found] after execution. However, execution of [[1]] && echo "successful" command in Bash Interactive mode is successful and results are as follows

[Email protected]:~$ [[1]] && echo "successful"


[Email protected]:~$

It seems that bash is supported [[Extended, so the problem should be on the script.]

3. Where does the problem in the script exist?

Obviously, the lone command is fine because it has been validated in interactive mode. There's another line in the script #!/bin/bash

Used to specify the type of shell used to run the script. Obviously, we're going to use bash here, so there's no problem with this line.

4. Since there is no problem with the contents of the script, where is the problem?

From the writing and running a few links to think carefully, since the script is not a problem, the question is not in the operation of the link? Out of habit, I often like $ sh test.sh such a way to run scripts, then, a different mode of operation can solve the problem? Run with./test.sh under Terminal, sure enough, run successfully! At this point, the crux of the problem is found.

4.1 The following question is why SH test.sh and./test.sh have different operating results.

By viewing (ls-l/bin), SH is just a symbolic link, and the final point is a program called Dash.

[Email protected]:~$ ls-hl/bin | grep sh

-rwxr-xr-x 1 root root 686K 2008-05-13 02:33 bash

-rwxr-xr-x 1 root root 79K 2009-03-09 21:03 Dash

lrwxrwxrwx 1 root root 4 2010-03-03 01:52 Rbash Bash

lrwxrwxrwx 1 root root 4 2010-03-03 01:53 sh, dash

lrwxrwxrwx 1 root root 4 2010-03-03 01:53 sh.distrib Bash

When you run SH test.sh, the SH command is called first, and sh points to dash, so sh test.sh is equivalent to/bin/dash test.sh. The dash, either the name or the program size, is different from bash. It is not surprising, then, that the SH test.sh and./test.sh Two commands have different execution results.

When the./test.sh command is executed, Bash automatically generates a subshell to execute the command, which means that executing the filename arguments is equivalent to executing the bash filename arguments.

4.2 The remaining question is, what is the difference between dash and bash?

The answer is given on the Ubuntu wiki. Since Ubuntu 6.10, the default shell/bin/sh of the system has been changed to dash. Dash (the Debian almquist shell) is a much smaller but still POSIX-compliant shell that consumes less disk space, executes shell scripts faster than bash, relies on fewer library files, and, of course, is not functionally comparable to bash. Dash comes from the NetBSD version of the Almquist Shell (ASH).

The main reason to change the default shell to dash in Ubuntu is efficiency. Because of the need to launch a large number of shell scripts during Ubuntu startup, Ubuntu has made such changes in order to optimize startup speed and resource usage.

4.3 How to avoid the problem caused by dash?

1. If dash affects only a few shell scripts, the most convenient workaround is to modify the script to specify the correct shell interpreter in the script

#!/bin/sh Change to #!/bin/bash

2. If dash affects the makefile file, you need to specify the following variables at the beginning of the file

SHELL =/bin/bash

3. If the scope of the impact is wide enough to modify a few separate files to resolve the problem, you can stop installing dash as/bin/sh

sudo dpkg-reconfigure Dash

It is important to note that such modifications will affect the boot speed of the system and may even affect some scripts that rely on Dash's unique features (which bash does not provide).

4.4 How do I avoid these effects when I write a script?

First, you can use the checkbashisms command to check if the script is "bash" (bashism). To use checkbashisms, you need to install the Devscripts package first

Aptitude Install Devscripts

Checkbashisms test.sh

Second, you need to be aware of the POSIX-compliant syntax when scripting, making the script "POSIX shell."

[[: Not Found error resolution in bash script]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.