Summary of various problems encountered from copying shell scripts from the Internet to running under linux

Summary of various problems encountered from copying shell scripts from the Internet to running under linux

Shortcut key Ctrl+U to view the source code of the web page
1. Error reporting phenomenon: ': No such file or directory

Solution: Execute under linux system: dos2unix filename

The cause of the problem: the essence is that the binary symbol ^M in the file is garbled

 Reference link: Four ways to drop ^M in Linux Develop Paper
2. Error reporting phenomenon: env: can't execute 'bash': No such file or directory

Workaround: Replace #!/bin/bash with #!/bin/sh

Cause of the problem: the essence is sh and bash problem

 Starting from ubuntu 6.10, ubuntu has replaced the previous default  bash shell with a dash shell , which shows that  the /bin/sh  link has reversed  /bin/dash  instead of the traditional  /bin/bash .

It can be seen through the ls -l /bin/*sh command:

So it prompts that bash cannot be found. The solution is to use bash instead of sh to run the script: bash test.sh or  replace #!/bin/bash with #!/bin/sh

3. Problems with garbled characters of various punctuation marks
1. Error reporting phenomenon: prompt xxx quot: not found

Solution: Replace " with "

The cause of the problem: the punctuation mark " is garbled, just replace " with "

2. && punctuation garbled characters: use && to replace garbled characters &&
3. >> garbled characters: use >> to replace garbled
characters & gt;> character >
Fourth, the error phenomenon: Syntax error: Bad for loop variable error solution

Reference link:

Syntax error: Bad for loop variable error solution

Syntax error: Bad for loop variable (Syntax error: Bad for loop variable)_Computer Training

Solution: Replace the for loop method with the while loop method

Cause of problem:for (( expr ; expr ; expr )) This C language format syntax isshnot available

 Starting from ubuntu 6.10, ubuntu has replaced the previous default  bash shell with a dash shell , which shows that  the /bin/sh  link has reversed  /bin/dash  instead of the traditional  /bin/bash .

It can be seen through the ls -l /bin/*sh command:

So when using the sh command to execute the script, dash is actually used, and dash does not support the for loop writing method in the C language format.

5. The meaning of eq, ne, le, ge, lt, gt in linux #!/bin/sh script

Reference link: https://www.cnblogs.com/hankyoon/p/12612549.html

if [ 1 -ne 1 ];then
...
fi

-eq: equal to
-ne: not equal to
-le: less than or equal to
-ge: greater than or equal to
-lt: less than
-gt: greater than

6. Error reporting phenomenon: syntax error: unexpected end of file (expecting "}")

Solution: Find the missing } symbol and add it 

Cause of the problem: { } symbols do not appear in pairs


 

Guess you like

Origin blog.csdn.net/a1809032425/article/details/131487689