System Configuration
1. Shell Script
Shell is an application that provides an interface for interaction between the user and the operating system kernel. Through a shell, users can send commands to the operating system, manipulate the file system, run programs, and perform various system tasks. Shells are typically command-line interfaces where users enter text commands to perform various tasks.
Common shells include sh, bash, csh, tcsh, ash, and more.
- sh (Bourne Shell): Sh is one of the most common command-line interpreters (shells) in Unix and Linux operating systems. It was named after its creator, Stephen Bourne, and is one of the earliest Unix shells.
- Bash (Bourne-Again Shell): Bash is the most commonly used shell in Unix and Linux systems. It is an improved version of the Bourne Shell, offering powerful scripting capabilities and a rich set of command-line tools.
- Dash: Dash stands for Debian Almquist Shell and is often used as the default shell for system initialization scripts. It is known for its fast startup and is suitable for system bootstrapping and script tasks.
- Csh (C Shell) and Tcsh (Tenex C Shell): These are variants of the C Shell and are primarily used in Unix systems. They have different syntax and features compared to Bash.
- Ash (Almquist Shell): Ash is another lightweight shell commonly used in embedded systems and some lightweight Linux distributions.
2. Creating Your First Script
Open a text editor, create a new text file, and name it luckfox.sh
. The file extension .sh
is commonly used to indicate a shell script, but the extension does not affect script execution.
Create the script and input the following code into
luckfox.sh
:#!/bin/sh
echo "Hello Luckfox !"- The
#!/
(shebang) line is a convention that tells the system which interpreter to use to execute the script, specifying which shell to use. In this case, it specifies/bin/sh
as the interpreter. - The second line uses the
echo
command to output the text "Hello Luckfox!" to the standard output (usually the screen). Running a command in a.sh
file is equivalent to running it directly in the terminal.
- The
Add execute permission to the script and run it:
# vi test.sh
# chmod 775 test.sh
# ./test.sh
Hello Luckfox !
3. Setting Up Autostart
In the /etc/init.d/
directory, you can find system scripts related to startup and shutdown processes.
When the system starts, it first executes rcS
, which loops through and executes the start
branch of scripts beginning with S
. When the system shuts down, it executes rcK
, which similarly loops through and executes the stop
branch of such scripts.To add a script that runs during system startup, follow these steps:
Create a new script with a name starting with
S??*
in the/etc/init.d/
directory.S
: Indicates that the script is for starting.??
: Two digits that represent the priority. Lower numbers indicate higher priority.*
: A descriptive string that describes the script's purpose.
Implement
start
andstop
branches in the script.Provide execute permission for the script:
chmod +x script_name
#!/bin/sh
case $1 in
start)
echo "start"
;;
stop)
echo "stop"
;;
*)
exit 1
;;
esacAdjust the priority of system scripts:
mv S99usb0config S90usb0config
mv S99_auto_reboot S90_auto_rebootFor example, to set an LED to remain on continuously after startup, you can name it S99LED (make sure this naming is after the system scripts).
#!/bin/sh
GPIO_PIN=55 # 设置GPIO引脚号
case $1 in
start)
echo "Starting..."
if [ ! -e /sys/class/gpio/gpio$GPIO_PIN ]; then
echo $GPIO_PIN > /sys/class/gpio/export
fi
echo out > /sys/class/gpio/gpio$GPIO_PIN/direction
echo 0 > /sys/class/gpio/gpio$GPIO_PIN/value
echo "Started."
;;
stop)
echo "Stopping..."
if [ -e /sys/class/gpio/gpio$GPIO_PIN ]; then
echo $GPIO_PIN > /sys/class/gpio/unexport
fi
echo "Stopped."
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
;;
esac- Use the
GPIO_PIN
variable to specify the GPIO pin number. - In the start operation, a conditional statement checks whether the GPIO pin has been exported to avoid duplication.
- In the stop operation, a conditional statement checks whether the GPIO pin has been exported to avoid duplication.
- Check the script's parameters to ensure that users only provide "start" or "stop" as arguments. If other arguments are provided, the script displays usage information and exits.
- Use the
4. Setting a Static IP
If you want to set a static IP address for a server and avoid random assignments from the router, you can configure a static IP. Be sure to:
- Use a static IP within the same subnet as your router.
- Avoid IP address conflicts with other devices assigned by the router.
#!/bin/sh
MAX_TRIES=10
TRIES=0
check_ip_address() {
if ifconfig eth0 | grep -q "inet "; then
return 0
else
return 1
fi
}
static_ip()
{
while [ $TRIES -lt $MAX_TRIES ]; do
if check_ip_address; then
echo "DHCP succeed!"
ifconfig eth0 192.168.10.66 netmask 255.255.252.0
route add default gw 192.168.11.1
echo "nameserver 114.114.114.114" > /etc/resolv.conf
ifconfig eth0 up
break
else
echo "wait DHCP IP..."
TRIES=$((TRIES + 1))
sleep 5
fi
done
if [ $TRIES -eq $MAX_TRIES ]; then
echo "error"
fi
}
case $1 in
start)
echo "start"
static_ip
;;
stop)
echo "stop"
;;
*)
exit 1
;;
esac
- In the statement ifconfig eth0 192.168.10.66 netmask 255.255.252.0, replace it with your own IP address and subnet mask.
- For the command route add default gw 192.168.11.1, please change the gateway address to your own configuration. If unsure, use relevant commands such as route -n to check.