Tag Archives: shell

Shell script error: Exec format error [How to Solve]

Question:

The script reports an error as follows

Nov 29 18:18:58 db53 systemd: Failed at step EXEC spawning /etc/rc.d/init.d/realserver: Exec format error
Nov 29 18:18:58 db53 systemd: realserver.service: control process exited, code=exited status=203
Nov 29 18:18:58 db53 systemd: Failed to start (null).
Nov 29 18:18:58 db53 systemd: Unit realserver.service entered failed state.
Nov 29 18:18:58 db53 systemd: realserver.service failed.

Solution:

Add at the beginning

#!/bin/sh

MAC terminal (shell) Error: You have mail [How to Solve]

background

You have mail will be prompted when you open the built-in shell or iterm on the Mac computer. I didn’t care before, because I am engaged in network security work. I usually operate and plan tasks when studying the Mac. I did test it before, but it has been cleared manually, but why is there such a prompt?

process

The overall process should be as follows: first, the mail mechanism of MAC itself, especially the problem of planned tasks, that is, if the machine planned tasks are not executed as expected, it will report errors, The error result will be sent to the user in the form of e-mail and stored in:/var/mail/user name. This file is also true for Linux. So when you open the shell, you will be reminded that there are emails here. Because the SH script executed in the planned task was deleted when the planned task was created, but the planned task itself was not deleted, so the planned task reported an error and sent a lot of emails. At this time, the message received is: you have new mail, but after the prompt is completed, After that, the prompt becomes you have mail. Use the mail command to view the specific mail content:

From [email protected]  Mon Aug 30 11:24:01 2021
X-Original-To: hait
Delivered-To: [email protected]
From: [email protected] (Cron Daemon)
To: [email protected]
Subject: Cron <[email protected]> /tmp/evil.sh
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=hait>
X-Cron-Env: <USER=hait>
Date: Mon, 30 Aug 2021 11:24:00 +0800 (CST)
/bin/sh: /tmp/evil.sh: No such file or directory

Analyze the reason: it can be judged from the time that it is the email in August, which makes sense at this time. The whole process:

1. Establish the planned task and run normally;

2. Delete the SH file executed by the planned task, resulting in an error message, and then send the e-mail.

3. Delete the planned task (stop sending the error message);

4. Message unread always prompt

Solution:

1. You can directly turn off the terminal mailbox function. The link above has the command (I failed, I used plan2)

2. Delete this folder:/var/mail/user name

 

The shell is compiled successfully, but an error is reported when running

The syntax on the shell is basically a glance and then used directly

What made me crazy at that time was the comparison of shells. I didn’t expect that there was a problem here

bash: [y: command not found…

After configuring environment variables

 source .bashrc

Bash: [y: command not found…

Check the syntax. The main reason is that there is no ” between [] and the judgment logic in the middle. After adding a space, there is no problem

Shell Error: bash: ./a.sh: /bin/bash^M: bad interpreter: No such file or directory [How to Solve]

The shell script written under Windows environment is uploaded to Linux for execution, and an error is reported

-bash: ./databasebak.sh: /bin/bash^M: bad interpreter: No such file or directory

It is suspected that there is a problem with the file format. Use vi databasebak.sh to enter the databasebak.sh file, and then execute it in the bottom mode  : set ff   Check it and find that fileformat = DOS. Look, it’s really a file format problem

Solution: after entering VI databasebak.sh, in the bottom mode, execute: set fileformat = UNIX and then execute: X or: WQ to save the changes. Then you can execute./databasebak.sh to run the script.

The shell script reported an error: not sure operator expected

A statement like this is used when matching strings

if[  $ timeofday  = ” yes”];   then

   echo  ” Good   morning”

   exit   0

The reason for the error is: if the value of the variable timeofday is empty, the if statement becomes if  [  =” yes”  ], This is not a legal condition. To avoid this, we must quote the variable if  [ “$ timeofdat”=”yes”  ], In this way, even null variables provide legal test conditions, if  [  ” “=” yes”   ]

Shell script execution error [: 0: unary operator expected

When executing the shell script, the if judgment statement reports an error

#!/bin/bash

if [ 0 == $testNum ]
then
    echo "Check Result:0"
fi

  

Testnum is not defined when it is used, and no assignment is empty (it was originally defined, but it is written incorrectly, similar to tsetnum). After the conversion, it is actually

if [ 0 ==  ]

Unequal and less [sign errors; Avoid the following

#!/bin/bash

if [[ 0 == $testNum ]]
then
    echo "Check Result:0"
fi

  Use “[[]]” to judge; Or add a character

#!/bin/bash

if [ "0"x == "$testNumx"x ]
then
    echo "Check Result:0"
fi

  

In fact, for my mistakes, of course, it’s better to use “[]” ordinary judgment, and you can check the mistakes; However, this implementation is mostly used to judge whether the query value or the value given by other conditions meet the conditions. It is a normal scenario if it is empty, so you should improve your code

Detailed meaning to be added

shell :syntax error: unexpected end of file

Sometimes an error is reported when executing a script:

[[email protected] shell]# sh -x test.sh 
+ $'\r'
: command not found
test.sh: line 37: syntax error: unexpected end of file

The reason may be:

Text editing is written through Notepad or other editors under windows. This file runs in cygwin simulated linux software environment

Solution:

The file format difference between dos and Linux

Text files under DOS take \ R \ n as the line break flag, expressed in hexadecimal, which is 0d 0A. Text files under UNIX take \ n as the line break flag, which is expressed in hexadecimal, which is 0A

[in windows, two symbols for line feed, enter \ R, line feed symbol \ n, only one symbol is required under Linux .]

Text files in DOS format will display ^ m at the end of the line when they are opened with a lower version of VI under Linux. Of course, they may not be visible, but when they are opened with VI, the format of this file will be displayed below, “m.txt” [DOS] 8L, 72C indicates that it is a DOS file format

Solution:

Use the following command to set the file format to UNIX format to solve the above error

vi test.sh
:set fileformat=unix
:wq

Why can’t scripts edited under windows be directly copied to UNIX system and run directly

Linux executes shell script and reports “syntax error near unexpected token ` in”

Old teletypewriters use two characters to start a new line. One character moves the carriage back to the first position (called enter, & lt; CR>, ASCII code is 0d), and another character moves the paper up one line (called line feed, & lt; LF>, ASCII code is 0A). When computers came out, memory used to be very expensive. Some people think it is unnecessary to use two characters to represent the end of a line. UNIX developers decide that they can use one character to represent the end of the line. Linux follows UNIX, which is also & lt; LF>。 Apple developers have specified & lt; CR>。 The guys who developed MS-DOS and windows decided to follow the old & lt; CR>& lt; LF>。

Because MS-DOS and windows use carriage return + line feed to represent line feed, use VIM to view the code written in VC under windows under Linux. The “^ m” symbol at the end of the line represents the symbol

shell :syntax error: unexpected end of file

 

Sometimes an error is reported when executing a script:

[[email protected] shell]# sh -x test.sh 
+ $'\r'
: command not found
test.sh: line 37: syntax error: unexpected end of file

The reason may be:

Text editing is written through Notepad or other editors under windows. This file runs in cygwin simulated linux software environment

Solution:

The file format difference between dos and Linux

Text files under DOS take \ R \ n as the line break flag, expressed in hexadecimal, which is 0d 0A. Text files under UNIX take \ n as the line break flag, which is expressed in hexadecimal, which is 0A

[in windows, two symbols for line feed, enter \ R, line feed symbol \ n, only one symbol is required under Linux .]

Text files in DOS format will display ^ m at the end of the line when they are opened with a lower version of VI under Linux. Of course, they may not be visible, but when they are opened with VI, the format of this file will be displayed below, “m.txt” [DOS] 8L, 72C indicates that it is a DOS file format

Solution:

Use the following command to set the file format to UNIX format to solve the above error

vi test.sh
:set fileformat=unix
:wq

[Solved] Shell Script [: -ge/-le/=/… : unary operator expected (standard_in) 1: syntax error

There was a syntax error when writing the script, but the result was correct

Error reporting behavior in script:

for (( i=0; i<=$ line1; i=i+1 ))
do
if [ $(echo “${R12S[i]} < 3” |bc) -eq 1 ] && amp; [ $( echo “${R23S[i]} > 3″|bc) -eq 1 ]\
&& amp; [ $( echo “${R13S[i]} > 3″|bc) -eq 1 ]; then
P1=P1+1
elif [ $(echo “${R12S[i]} > 3” |bc) -eq 1 ] && amp; [ $( echo “${R23S[i]} > 3” |bc) -eq 1 ]\
&& amp; [ $( echo “${R13S[i]} < 3″|bc) -eq 1 ]; then
P2=P2+1
elif [ $(echo “${R12S[i]} > 3” |bc) -eq 1 ] && amp; [ $( echo “${R23S[i]} < 3” |bc) -eq 1 ]\
&& amp; [ $( echo “${R13S[i]} > 3” |bc) -eq 1 ]; then
P3=P3+1
elif [ $(echo “${R12S[i]} > 3” |bc) -eq 1 ] && amp; [ $( echo “${R23S[i]} > 3” |bc) -eq 1 ]\
&& amp; [ $( echo “${R13S[i]} > 3” |bc) -eq 1 ]; then
P4=P4+1
fi
done

The error is as follows:

(standard_ in) 1: syntax error
path.sh: line 84: [: -eq: unary operator expected
(standard_ in) 1: syntax error
path.sh: line 87: [: -eq: unary operator expected
(standard_ in) 1: syntax error
path.sh: line 90: [: -eq: unary operator expected
(standard_ in) 1: syntax error
path.sh: line 93: [: -eq: unary operator expected

Use the SH – C path.sh command to check whether there are syntax errors in the script
or not

However, there is no error, indicating that the script itself is not a problem, the problem should be in the run time

Baidu reported an error [: – EQ: unary operator expected], some netizens said it was because the condition after if had to be double []

So all the conditional statements were modified, but the error was still reported

(standard_ in) 1: syntax error

(standard_ in) 1: syntax error

(standard_ in) 1: syntax error

(standard_ in) 1: syntax error

Later, I found that it was the problem of circulation

for (( i=0; i<=$ line1; This cycle starts from 0 and ends at $Line1, which is equivalent to $Line1 + 1 cycles

However, there is only $Line1 number in the array, so one number is missing, so an error will occur. This also proves why the running result is correct, because the last number is empty, which will not affect the previous number

Modify: change for ((I = 0; i<=$ line1; I = I + 1) is changed to for ((I = 0; i<=$ line1-1; If I = I + 1), there will be no error message when running again

After checking the information again, it is found that many people encounter this type of error reporting:

[: – Ge/- Le/= /…: unary operator expected error

Error reason:

Since the initialization value of the variable rate is empty, it becomes [- Ge “10”]. Obviously [is not compared with “10” and lacks [symbol], so this error is reported

Solution:

1. Check whether the assignment is null due to the wrong writing of the assignment statement

2. Add declare – I rate = 0 before assignment

3. Change it to if [[$rate – Ge 10]] and add a pair of []