Tag Archives: shell

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~~~

Shell script error: ‘[: =: unary operator expected’

Shell script error: [: =: unary operator expected “2009-08-11 18:00

When the matching strings are equal, I use a statement like this: if [$status = = OK]; Then echo “OK” fi in the run-time [: =: unary operator expected error, has been unable to find the reason, tried to delete the space on both sides of the equal sign and the space in brackets do not work, finally Baidu found the reason. Change the statement so that there is no error: if [[$status = $OK]]; The reason is that if the value of the variable status is empty, it will become [= “OK”]. Obviously [it is not equal to “OK” and lacks [symbol], so this error is reported. Of course, errors are not always made. If the value of the variable status is not empty, the program will be normal, so such errors are very hidden. Or the following method can be used to avoid this error: if [“$status” x = = “OK” x]; Of course, X can also be other characters. By the way, whether there are double quotes in the shell is consistent in many cases

Solve the problem of shell script “syntax error near unexpected token ` fi ‘”.

When executing the shell script, the following error is prompted:

After searching for information, I found:

carried out:

1
vi finddir.sh

Then, enter

1
set ff

The results are:

The solution is to modify it to unix:

1
set ff=unix

Execute the save command:

1
:wq

Execute again:

1
:set ff

Finally execute the command.

There is an error in this article, that is in the script

1
if [-d "$folder"]; then

To:

1
if [ ! -d "$folder" ]; then

Pro-test so good.

 

Shell script error newline syntax error near unexpected token

Sometimes some sh script files that are not handled properly in windows are put into Linux to execute, and an error is reported, indicating the existence of newline character

The main reason is that the end of EOL line in window is two characters, while in Linux and UNIX it is single character

 

Confirm with the VIM command below

#Display the carriage return character (\r), specifically in the vim environment it will be displayed as the ^M symbol
:e++ff=unix%

#Display the line feed (\n), specifically the $ symbol in the vim environment.
#This command also displays tabs, which are displayed in the vim environment as ^I symbols
:setlist

The above two VIM commands can also be viewed through a shell command

cat-Afilename

 

 

Delete the carriage return to make the shell script run normally

:%s/\r//g

You can also convert windows format file to Linux format file by shell instruction

dos2UNIXfilename

 

Attached:

Converting Linux format file to windows format file

UNIX2dosfilename