Tag Archives: shell

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 []

Python calls shell script: oserror: [errno 8] exec format err

Knowledge map advanced must read: read how large-scale map data efficient storage and retrieval>>>

Recently, we encountered a script call problem in the company project. A python command-line tool started a zookeeper shell script using the subprocess module, and reported an error during execution: oserror: [errno 8] exec format error, as shown in the figure below:

At this point, I try to execute the shell script in the red box on the command line : is executed successfully, indicates that it is not a shell script problem

Then, I suspected that there was a problem with the permission of Python. I checked it online and said that I wanted to add a Python interpreter ( #)/ Usr/env/Python ), so I checked the content of the corresponding Python file: confirm that it contains / usr/env/python


Then I worried about the issue of execution permission , so modified the executable permission of Python and shell scripts: but still did not solve the problem

chmod+xXXX.pychmod+x/etc/init.d/zookeeper

Finally, I suspect that there is something wrong with the shell script interpreter . At this time, I open the shell script (/etc/init. D/zookeeper ) and find that there is no / bin/sh

As we know, shell scripts can be executed in two ways:

1. sh XXX.sh

2. chmod +XXX.sh; ./ XXX.sh

For the second mode of operation, the script must contain a shell interpreter

As shown in the figure below, I added a shell interpreter (#)/ After that, the python script calls the shell script and does not report an error

So far, the problem has been solved

The debugging process is divided into three steps

1. Execute the shell script manually: see if the script itself has problems, and ignore the problem of calling the shell by python

2. Check the executable permissions: both shell and python scripts should ensure that the user has executable permissions. If you don’t feel at ease, use the script corresponding to Chmod + X

3. Check the interpreter of the script: because the script is an interpretive language (you need to specify an interpreter to execute line by line), it is not a compiled language (you don’t need to translate and interpret line by line after compiling). If there is no interpreter, the script may not be executable

Blogger: testing makes money

Motto: test to complete the original accumulation, invest to financial freedom

csdn: https://blog.csdn.net/ccgshigao

Blog Park: https://www.cnblogs.com/qa-freeroad/

51cto: https://blog.51cto.com/14900374

In this article, we share the blog “test makes money” (51CTO).

When Linux runs SHELL file, $’\ R’: command not found appears

When running the written shell script, the error prompt $'\ \ R': command not found appears

The reason for the error is that when we operate in Windows system, the newline character in the editor is \\\\\\\\\\\\\\\\\

There are two ways to deal with it

1. VI command to enter the file to be edited, press ESC key to enter the command mode, execute the following command, press enter key, and then save to exit the file

: set ff=unix

2. The second kind

yum -y install dos2unix
dos2nuix install.sh

The above install. SH is the script you wrote. Use dos2nuix to process the files

Shell Syntax Error: operand expected (error token is “-”)

In this script for monitoring the real-time network port rate, there is an error on lines 21 and 22:

#!/bin/bash
#Modified by [email protected]
echo ===DTmobile NetSpeedMonitor===
sleep 1
echo loading...
sleep 1

ethn=$1
 
while true
do
  RXpre=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $2}')
  TXpre=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $10}')
  sleep 1
  RXnext=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $2}')
  TXnext=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $10}')
 
  clear
  echo -e "\t\t\t  RX \t\t   TX  \t\t\t TIME"
 
  RX=$((${RXnext}-${RXpre}))
  TX=$((${TXnext}-${TXpre}))
 
  if [ $RX -lt 1024 ];then
    RX="${RX}B/s"
  elif [ $RX -gt 1048576 ];then
    RX=$(echo $RX | awk '{print $1/1048576 "MB/s"}')
  else
    RX=$(echo $RX | awk '{print $1/1024 "KB/s"}')
  fi
 
  if [ $TX -lt 1024 ];then
    TX="${TX}B/s"
  elif [[ $TX -gt 1048576 ]];then
    TX=$(echo $TX | awk '{print $1/1048576 "MB/s"}')
  else
    TX=$(echo $TX | awk '{print $1/1024 "KB/s"}')
  fi
 
  echo -e "$ethn \t $RX   $TX   \t\t\t `date +%k:%M:%S` "
 
done

Revised document

#!/bin/bash
#Modified by [email protected]
echo ===DTmobile NetSpeedMonitor===
sleep 1
echo loading...
sleep 1

ethn=$1
 
while true
do
  RXpre=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $2}')
  TXpre=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $10}')
  sleep 1
  RXnext=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $2}')
  TXnext=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $10}')
 
  clear
  echo -e "\t\t\t  RX \t\t   TX  \t\t\t TIME"
 
  RX=$((RXnext-RXpre))
  TX=$((TXnext-TXpre))
 
  if [ $RX -lt 1024 ];then
    RX="${RX}B/s"
  elif [ $RX -gt 1048576 ];then
    RX=$(echo $RX | awk '{print $1/1048576 "MB/s"}')
  else
    RX=$(echo $RX | awk '{print $1/1024 "KB/s"}')
  fi
 
  if [ $TX -lt 1024 ];then
    TX="${TX}B/s"
  elif [[ $TX -gt 1048576 ]];then
    TX=$(echo $TX | awk '{print $1/1048576 "MB/s"}')
  else
    TX=$(echo $TX | awk '{print $1/1024 "KB/s"}')
  fi
 
  echo -e "$ethn \t $RX   $TX   \t\t\t `date +%k:%M:%S` "
 
done

The reason is that when the value of $is taken, only variables need to be followed in the brackets (variables can be calculated by themselves), and it is not necessary to take the value of variables in the brackets

RX=$((${RXnext}-${RXpre}))
  TX=$((${TXnext}-${TXpre}))
  
change to:
  
  RX=$((RXnext-RXpre))
  TX=$((TXnext-TXpre))

And then there’s no error~~~