0% found this document useful (0 votes)
9 views

TP Linux 3

Uploaded by

Mariem Arbi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

TP Linux 3

Uploaded by

Mariem Arbi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

ENISO AU : 2024-2025

Enseignante : Mariem Arbi

TP de système d'exploitation linux

TP 3 : Scripting introduction

Shell scripting is a text file with a list of commands that instruct an operating system to perform
certain tasks. A shell is an interface that interprets, processes, and executes these commands from the
shell script. It can be particularly helpful to automate repetitive tasks, helping to save time and reduce
human error.
What is shell scripting used for?

Shell scripting is primarily used to automate repetitive system tasks, such as backing up files,
monitoring system resources, and managing user accounts. By turning a series of commands into a
script, system administrators can save time, increase accuracy, and simplify complex tasks.
Basic shell scripting terms

Before we delve further into shell scripting, knowing a few definitions may come in handy when
exploring this topic:

• Terminal: A terminal is a program that establishes a connection with the server.

• Shell: This program interprets shell scripting commands from the terminal and runs on
the server. This is the command-line user interface you choose and includes shells such
as the Bourne shell, Korn shell, Bourne-Again shell, and C shell.

• Script: A script is a short program that performs a specific task.

• Command-line shell: A command-line shell (also known as a command prompt) allows


you to instruct your computer through textual commands.

• Shell script: A shell script is a script run through a command-line shell

When you create a shell script, you might use text editors like Nano or Vim and save the file with
a “.sh” extension. A shell interpreter interprets and executes these scripts; you can run them
from the command line whenever necessary.

One of interesting applications to use script shell is robotic application. Creating shell scripts for
robotic applications is to automate and streamline various processes involved in controlling,
managing, and monitoring robots. Shell scripts are powerful tools for automating tasks in the
Linux/Unix environment, which is commonly used in robotics due to its flexibility and open-
source nature. Shell scripts in robotic applications serve to improve efficiency, reliability, and
scalability by automating tasks, simplifying the management of complex systems, and providing
a robust interface for interacting with robots. They can also reduce the likelihood of human error,
facilitate easier debugging, and make system maintenance more efficient. For developers and
engineers working with robotics, shell scripting is a foundational skill that supports the
deployment, testing, and continuous operation of robots in real-world environments.

Page 1 sur 6
ENISO AU : 2024-2025

Here are some of the primary reasons and benefits for using shell scripts in robotic
applications:

1. Automation of Repetitive Tasks:

• Routine Operations: Robots often perform tasks that need to be repeated, like starting,
stopping, or resetting software, moving parts, or logging data. A shell script can
automate these repetitive tasks, making the robot easier to control and manage.

• Sequence of Commands: Robotic systems often involve a series of complex steps (e.g.,
calibrating sensors, starting the motor, activating communication protocols, etc.), which
can be easily scripted to run automatically in the correct sequence.

2. Efficient System Configuration:

• Initial Setup: When deploying a robotic system, there may be several configuration
steps (setting up ROS nodes, network configurations, environment variables). A shell
script can set up these parameters automatically, ensuring consistency and reducing
human error.

• System Initialization: It can launch the required processes, services, and background
tasks (like robot control software, vision systems, and sensor data processing) in a
specific order with one command.

3. Integration with Other Software:

• Communication with Hardware: Many robotic systems integrate with other software
tools or middleware platforms (e.g., ROS, OpenCV, TensorFlow). A shell script can be
used to initiate and manage these connections and ensure that all necessary
dependencies are in place.

• Interfacing with External Systems: Shell scripts can also facilitate communication
between the robot and other systems (like cloud servers or other robots) via network
protocols (HTTP, MQTT, etc.).

4. Data Logging and Monitoring:

• Logging System Data: Robotics often requires continuous logging of performance


metrics, sensor data, and status reports for analysis and debugging. Shell scripts can
automate this process, collecting data at specified intervals and storing it in files for
future review.

• Health Monitoring: Shell scripts can check the status of the robot's components (e.g.,
motors, sensors, or network connections), and if any component fails or malfunctions,
the script can send alerts or trigger troubleshooting actions.

5. Error Handling and Debugging:

• Error Detection: If a robot encounters an issue, a shell script can be used to check for
common error states (e.g., low battery, disconnected sensor) and attempt corrective
actions or log error messages.

• Debugging and Recovery: Shell scripts can automatically restart certain subsystems,
reset parameters, or provide logs to help developers debug issues in real-time.

Page 2 sur 6
ENISO AU : 2024-2025

6. Task Scheduling and Remote Control:

• Task Scheduling: Robotic tasks may need to be performed at specific times or intervals.
Shell scripts can automate this scheduling, for example, using cron or other scheduling
utilities.

• Remote Operation: Shell scripts can facilitate remote control by executing commands
on a robot via SSH or other remote interfaces, enabling operators to manage the robot
from anywhere.

7. Simplified Interaction:

• User-Friendly Interfaces: A well-written shell script can abstract complex robotic


operations into simple commands that can be run by non-experts. For example, instead
of manually executing each command for starting or stopping the robot, the operator can
run a single shell script that handles everything.

• Prototyping and Testing: When developing new robotic applications, shell scripts can
be used to quickly test different configurations, algorithms, or parameters without
requiring the full user interface or manual intervention.

8. Cost-Effectiveness and Flexibility:

• Low Overhead: Shell scripts are lightweight, easy to write, and have minimal
performance overhead, making them ideal for small and resource-constrained systems
that cannot afford the complexity of a full-fledged GUI-based interface.

• Cross-Platform Compatibility: Shell scripts written for Unix-like systems


(Linux/macOS) can often run on a wide variety of platforms with minimal modification,
making them portable across different robotic hardware configurations.

9. Safety and Shutdown Procedures:

• Graceful Shutdown: If a robotic system is running autonomously, a shell script can


ensure that the system shuts down safely, stopping all motors, sensors, and software
services in the correct order to prevent damage or accidents.

• Emergency Stops: In case of an emergency or fault, shell scripts can be set up to


immediately halt the robot's operation, either manually triggered by the operator or
automatically in response to a critical error.

10. Interfacing with Sensors and Actuators:

• Sensor Data Processing: Shell scripts can help process data from various sensors and
use it to make real-time decisions. For example, a script might fetch data from a depth
sensor, process it, and trigger specific movement commands based on the input.

• Control Signals to Actuators: Commands to actuators (e.g., motors, grippers) can be


sent via shell scripts, often calling specific programs or APIs designed to interface with
the robot's hardware.

Page 3 sur 6
ENISO AU : 2024-2025

Introduction to vi :

vi file_name or vim file_name yw:copy one word


Command mode and insert mode 5yb:copy five words back
Esc (échap) : back to command mode 7dw: delete seven words
Switch to insert mode: start typing Save or not:
I: start typing at the start of current line Be sure to be in command mode not insert mode
i: start typing before the current character :w : save
A: start typing at the end of the current line :w file_name : save as file name
a: start typing after the current character :q : quit
O: start typing on a new line before current line :wq : save and quit
o: start typing on a new line before the current line :q! quit and discarding changes
replace and delete a character :w! save and quit
Be sure to be in command mode not insert mode
x: delete the character below the cursor Searching
X: delete the character before the cursor Be sure to be in command mode not insert mode
r: replace the character below the cursor /string : forward searching for string
p: paste after the cursor ?string : backward search for string
xp: switch two characters n: go to next occurrence of searching string
undo and repeat /^string: forward search string at beginning of line
u : undo the last action /string$: forward search string at end of line
. : repeat the last action /kg[aert]u : search for kgau kgeu kgru kgtu
Cut, copy, past a line /\<he\> search for word he
Be sure to be in command mode not insert mode Replace all
dd:cut the current line In command mode
yy:copy the current line :4,8 s/foo/bar/g : replace foo with bar on lines 4 to 8
p: paste after the current line :1,$ s/foo/bar/g : replace foo with bar on all lines
P: paste before the current line Reading files:
Cut copy and paste lines In command mode
Be sure to be in command mode not insert mode :r fname : read file and paste contents
Number to repeat the command + dd: cut lines : 2dd :r !cmd execute cmd and paste its outputs
Number to repeat the command + yy: copy lines : 2yy :n start editing the next file
Start and end of line: :e toggle with the last edited file
Be sure to be in command mode not insert mode Script:
0: jump to start of current line Shebang: Scripts usually start with a shebang line
^: jump to start of current line (#!/bin/bash), indicating the script should be run with
$: jump to end of current line Bash.
d0:delete until start of line echo helloworld
d$:delete until end of line echo echo hello world > hello_world
Join two lines vi echo hello world > hello_world
Be sure to be in command mode not insert mode chmod +x hello_word make the script executable : don’t
J: join two lines forget that
yyp:duplicate a line ./hello_world : execute the command
ddp:switch two lines #!/bin/bash
words #hello world script
Be sure to be in command mode not insert mode #
W: jump to the next word echo hello world
b:back one word
3w: forward three words
dw: delete one word

Page 4 sur 6
ENISO AU : 2024-2025

variables for loop combined with embedded shell


#simple variable in script for counter in ‘seq 1 20’
Var=4 do
Echo $var echo counting from 1 to 20 , now at $counter
Troubleshooting a script sleep 1
Another way to run a script in a separate done
bash runme while loop
bash -x runme : bash -x allows you to see the i=100
commands that the shell is executing while [$i -ge 0]
Scripting loops: do
Test: echo Counting down, from 100 to 0, now at $i;
test 56 -gt 55 ; echo $? let i--;
The command test returns 1 if the test fails and returns done
0 if test succeeds until loop
If you want to return true and false: you can write your let i=100;
code like this: until [$i -le 0]
test 56 -gt 55 && echo true || echo false do
in brackets : echo Counting down, from 100 to 1, now at $i;
test [56 -gt 55] && echo true || echo false let i--;
more option for tests: done
man test Evaluation of numerical expression (())
combined with logical AND and OR (( 42>43)) && echo true || echo false
[56 -gt 55 -a 66 -lt 500] && echo true || echo false var42=42
[56 -gt 55 -o 66 -lt 500] && echo true || echo false ((42=var42)) && echo true || echo false
If then else: ((42=$var42)) && echo true || echo false
Write in choice let x=”3+4” ; echo $x
If [ -f isit.txt] let x="0xFF" : let interprets "0xFF" as a hexadecimal
then echo isit.txt exists! number and performs an arithmetic operation to assign it
else echo isit.txt not found! to the variable x.
Fi let x="0xFF" ; echo $x
./choice file help.sh
touch isit.txt #wild animals helpdesk advice
echo -n " what animals did you see ? "
read animal
if then elif case $animal in
/bin/bash "lion" | "tiger" )
count=42 echo “ you better start running fast "
if [$count -eq 42] ;;
then "cat")
echo “42 is correct” echo "let that mouse goes"
elif [$count -gt 42] ;;
then "dog"
echo “too much” echo "don’t woeey, give it cookies")
else ;;
echo “not enough” "chiken" | "goose" | "duck"
fi echo "eggs for breakfast"
for loop ;;
for i in 1 2 4 "liger")
do echo “approach and say ‘ah you big bluffy kitty
echo $i ….."
done ;;

Page 5 sur 6
ENISO AU : 2024-2025

"babelfish”)
Echo "dit it fall out your ear"
;;
*)
echo "you discovered unknown animal name
it"
;;
esac
./help.sh

Page 6 sur 6

You might also like