Tag Archives: shell

[Solved] Linux shell Script Error: syntax error near unexpected token `do

[problem] the error content reported by the Linux server running the shell script is shown in the following figure.

[reason] incompatible carriage return and line feed characters under Linux and windows

[solution] change the windows newline character “CR lf” in the file to UNIX “LF”

Use Notepad + + to set the view first -> Display symbol -> When all symbols are displayed, you will see “CR lf”

Edit -> with Notepad + +; Document format conversion -> Convert to UNIX (LF)

Re-run to solve the problem.

[Solved] Shell loop execute error: syntax error: bad for loop variable

#!/bin/bash

for ((i = 10 ; i>=0 ; i--))
do
	echo $i
done

Syntax error: bad for loop variable

The reason is that since Ubuntu 6.10, Ubuntu has replaced the previous default bash shell with dash shell; It shows that the/bin/sh link reverses/bin/dash instead of the traditional/bin/bash.

Question 1: why #/ Bin/bash doesn’t work?

./execution will read #/ Bin/bash specifies the shell parser, and the script needs execution permission

Executing with SH will not read #/ Bin/bash, which is equivalent to executing the/bin/sh shell script and passing it in as a parameter. The script does not need execution permission, but only reading permission

Solution 1:

Execute sudo dpkg reconfigure dash and select No

Solution 2:

Modify cycle


for i in `seq 1 10`                                                                                                
do                                                                                                               
        echo $i                                                                                                  
done                                                                                                             
         
OR
                                                                                                        
for i in {0..10}                                                                                                 
do                                                                                                               
        echo $i                                                                                                  
done  

Linux Command Error: Redis (error) NOAUTH Authentication required

The authentication problem occurs because redis has set the authentication password
after startup, enter the following command:

127.0.0.1:6379> auth yourpassword

My password is redis123

127.0.0.1:6379> set name hello
(error) NOAUTH Authentication required.
127.0.0.1:6379> (error) NOAUTH Authentication required.
(error) ERR unknown command '(error)'
127.0.0.1:6379> auth redis123
ok

Redis can be operated normally.

Memory error: cannot allocate memory [How to Solve]

1. Problem background

Start a process, and the process reports an error: cannot allocate memory

2. Cause of problem

Check the script of this process. It is found that the script needs to call the memory of the system kernel to start, but the current kernel memory is not allocated.

3. Troubleshooting

1. View the remaining memory of the current physical machine

free -m

2. View the number of processes in the current system

# The maximum number of processes allowed in the system
sysctl kernel.pid_max

# The maximum number of processes on the current host
ps -eLf | wc -l

3. View memory application and availability

cat /proc/meminfo | grep Commit

4. Solution

Unable to allocate kernel memory

sysctl overcommit_memory=1

[Solved] curl: symbol lookup error: curl: undefined symbol: curl_mime_free

curl: symbol lookup error: curl: undefined symbol: curl_mime_free
Execute curl command to report error

curl -s https://www.adas.com

report errors:

curl: symbol lookup error: curl: undefined symbol: curl_mime_free

Solution:

#ubuntu system
apt-get update -y

#centos
yum update -y

Execute curl – s again, normal

[Solved] The Shell Script error: syntax error: “(” unexpected “)

Execute the shell script with the following error messages

This is related to the version of shell you actually use. It can be printed with LS - L/bin/*sh, for example:

Here, SH is redirected to dash, so if sh positionpara.Sh, dash is used. There are many ways to avoid error reporting, such as executing bash positionpara.Sh, or executing ./positionpara.Sh .

You have 77 PMD violations maven error [How to Solve]

Maven compilation exception

Failed to execute goal org.apache.maven.plugins:
maven-pmd-plugin:3.8:check (default) 
on project xxx: You have 133 PMD violations. 

Reason: PMD verification is added to Maven to judge whether your code complies with the specification. If it does not comply with the specification, an error will be reported when Maven compiles

Solution: how can this specification be removed? Just execute the following command, which can ignore the PMD check

mvn clean install -Dpmd.skip=true -Dcheckstyle.skip=true

Of course, when Maven compiles, there will be unit test execution, so how to ignore unit test execution?Just execute the following command

mvn clean install -Dmaven.test.skip=true

Therefore, as long as you want to ignore it, you can execute a command directly behind it
in a word:

In order to be compatible with errors, the following methods can be performed:

mvn clean install -Dpmd.skip=true -Dcheckstyle.skip=true -Dmaven.test.skip=true

[shell] sh executes the script and reports an error syntax error: “(” unexpected “)

Question:

The SH script contains the contents of array initialization

$ str="123 456 789"
$ array=($str)
$ echo ${array[2]}

SH executing the script will report an error  Syntax error: "(" unexpected

reason:

Other common Linux distributions, although many point sh to bash

The SH command on Debian/Ubuntu points to dash instead of bash by default

And because dash is lighter than bash, it only supports basic shell functions,  

It does not include the array initialization just now, so it can not be recognized. Syntx error is directly reported

solve:

  The solution is to use it directly   bash test. Sh , or ./test. Sh , these two ways to execute the script.


When executing sh./xxx.sh, the solution of “syntax error:” (“unexpected”) appears

  Yesterday, the system identification part of virtualmin was updated to enable it to support one click installation and optimization under Debian system. The code was almost modified. Reinstall the VPS into Debian. Execute the code through sh./virtualmin.sh and report “syntax error:” (“unexpected”) Error. It’s OK to execute through bash./virtualmin.sh. After searching the syntax for several times, there’s no problem. Later, find the cause of the problem on the Internet:
        The code is correct for standard bash, because Ubuntu/Debian uses dash instead of traditional bash in order to speed up startup. Dash is making trouble. The solution is to cancel dash.

solve:

1) sudo dpkg-reconfigure dash         Select no from the options and it’s done!

2) Execute dpkg reconfigure dash under root and select No

zxl@ubuntu :~/package$ su
Password:
root@ubuntu :/home/zxl/package# dpkg-reconfigure dash


file_name.sh: 3: Syntax error: “(“ unexpected [How to Solve]

An error was encountered

Execute the command $./example03. Sh error:
./example03. Sh: 3: syntax error: “(” unexpected
check the document

#!/bin/sh
#function hello
function hello(){
    echo "hello SXU."
}
#main
hello	
exit 0

Confirm again and again that there is no error.

Find the reason

Related to the actual shell version used
use the command LS - L/bin/* sh to print and view. For example:
you can see that SH is redirected to dash. Therefore, if./example.sh is executed, dash is used.

Solution:

bash example. Sh the first line of the script #/ Change bin/sh to: #/ Bin/bash, and then execute./example. Sh. The code is correct for standard bash, because Ubuntu/Debian uses dash instead of traditional bash in order to speed up startup. The solution is to cancel Dash: sudo dpkg reconfigure dash , and select no in the selection, choose one of the three methods to succeed.

Error: EACCES: permission denied, unlink ‘xxxx/xxxx/xxxx‘ [How to Solve]

Scenario: on the Mac, if you want to use the code command to open vs code, (CMD + Shift + P) search the shell, find this:

Results Click to find:

	Error: EACCES: permission denied, 
	unlink '/usr/local/bin/code'

In fact, I don’t have read-write permission, and it’s ineffective to search a lot on the Internet. However, I’m used to using the command line to open vs code. The final solution is:

Enter at the command line

sudo chown -R [yourusername] [filePath]

Yourusername: user preferences – & gt; Users and groups – & gt; Current user
filepath: the path displayed when an error is reported

Tips: if not, try to move the path forward. Take me as an example:

sudo chown -R xxx /usr/local/bin