Raspberry Pi_Eng_20.2.6 Running Script


Published Book on Amazon


All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 1
All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 2


출판된 한글판 도서


최신 라즈베리파이(Raspberry

Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (상)

최신 라즈베리파이(Raspberry

Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (하)


Original Book Contents


20.2.6  Running Script

 

20.2.6.1    Preparing to Run Script File

 

A script is not a binary executable like a normal executable program, but it must be executable as a program itself. Therefore, the execute permission must be granted to the script file. You can use "chmod" command to grant the desired permission. For the permissions of a file, see the description of [13.2 Changing Permission on File].

 

Write the script as follows and save it to "home/pi/test_run.sh" file in your system.

 

#! /bin/bash

echo "This is running test"

                                                    

pi@raspberrypi ~ $ cd Script

pi@raspberrypi ~/Script $ ls  -l test_run.sh

-rw-r--r-- 1 pi pi 30 Apr 29 16:43 test_run.sh

 

In this state, enter the file path and script file name and press [Enter] to execute the script.

 

pi@raspberrypi ~/Script $ ./test_run.sh

-bash: test_run.sh: command not found

 

Unfortunately, the system display the message that Bash can not execute the command. Why does this error occur? This is because the file is not in a executable state. If you look at the permission information of "test_run.sh" file above, you can see that there is no execute permission.

 

Use "chmod" command as follows to grant execute permission to the script file, and check the result.

 

pi@raspberrypi ~/Script $ chmod +x test_run.sh

pi@raspberrypi ~/Script $ ls  -l test_run.sh

-rwxr-xr-x 1 pi pi 30 Apr 29 16:43 test_run.sh

 

Now run the script again. You can notice that the script runs normally.

 

pi@raspberrypi ~/Script $ ./test_run.sh

This is running test

 


 

20.2.6.2    Running Script File

 

You can execute the script file basically in the same way as a normal Shell command. You can run it manually by entering it on the Terminal screen, or you can run it from within another script.

 

Generally, when running a script file, you must specify both directory and file name. Even if the script file is in the current working directory, you must specify the directory as "./script-file". This is because Shell does not search the current working directory when it finds the command. If you want to let the system search the current working directory by default, you need to add the current working directory "./" to the environment variable PATH.

 

We will test it using the "test_run.sh" script created earlier. Try to run the script as follows with the current working directory "~/Script". Then, an error occurs, which means that Bash can not find "test_run.sh". It tells that even if the current working directory is in "~/Script", it will not find that file.

 

pi@raspberrypi ~/Script $ test_run.sh

-bash: test_run.sh: command not found

 

This time, we try to specify the current working directory clearly and execute it again. Now, the script runs normally.

 

pi@raspberrypi ~/Script $ ./test_run.sh

This is running test

 

Generally, when Shell executes a command, it uses the path specified by the environment variable PATH to find the location where the command actually exists, and if it can not find the command, it will not execute it. Below is checking what value is in the environment variable PATH.

 

pi@raspberrypi ~/Script $ echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games

 

If you look at the above value, there is no current working directory. If you want Shell to always find the current working directory, you need to append "./" that means the current working directory.

 

Next is an example that use "export" command to add the current working directory "./" to the environment variable PATH and check it again.

 

pi@raspberrypi ~/Script $ export PATH="$PATH:./"

pi@raspberrypi ~/Script $ echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games:./

 

Now, let's just run the script file name purely without specifying the file path. Then you can see that it runs normally. That is, the path "./" specified in PATH is applied.

 

pi@raspberrypi ~/Script $ test_run.sh

This is running test