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/shas the interpreter. - The second line uses the
echocommand to output the text "Hello Luckfox!" to the standard output (usually the screen). Running a command in a.shfile 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
3.1 Buildroot Environment
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
startandstopbranches 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
;;
esac
4. Configuring a Static IP
4.1 Direct Connection via Ethernet Cable
In this scenario, the development board and the computer can communicate within the same local network but cannot access the internet or external networks.
Configure the
IPaddress, subnet mask, and gateway for the development board'seth0interface.ifconfig eth0 192.168.10.200 netmask 255.255.252.0
route add default gw 192.168.11.1
echo "nameserver 114.114.114.114" > /etc/resolv.conf- Note: The settings configured in this step will reset after a reboot. If there is no need for internet connectivity and you want to avoid manual configuration each time, you can create a startup script.
Write a Script and Assign Executable Permissions:
vi /etc/init.d/S99static#!/bin/sh
case $1 in
start)
ifconfig eth0 192.168.10.200 netmask 255.255.252.0
route add default gw 192.168.11.1
echo "nameserver 114.114.114.114" > /etc/resolv.conf
;;
stop)
;;
*)
exit 1
;;
esacchmod +x /etc/init.d/S99static
4.2 Connecting the Development Board to a Router or Switch
To configure a static IP address, for example, create a script named
S99staticip(ensure the script is executed after system initialization scripts).- Ensure the development board is connected to the router or switch, and the static IP address falls within the router's subnet.
- Avoid IP address conflicts with addresses already 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- Replace the IP address and subnet mask in the command
ifconfig eth0 192.168.10.66 netmask 255.255.252.0with your specific values. - For the command
route add default gw 192.168.11.1, update the gateway address based on your configuration. If uncertain, use commands likeroute -nto verify the gateway.