It is possible to write a bash script and have a function called automatically when the script is run. This way, it can be just like a C program. This is a very interesting way to code a bash script. and I also found a way to have an array of bash functions and run them one after another. This should be very helpful.
#!/bin/bash get_os_info() { echo -n "OS: " cat /etc/os-release | grep PRETTY_NAME | cut -d '=' -f 2 | tr -d '"' } get_bash_info() { printf "Bash version: %s\n" ${BASH_VERSION%%[^0-9.]*} } get_kernel_version() { echo -n "Kernel: " uname -r } get_cpu_info() { echo -n "CPU: " grep 'model name' /proc/cpuinfo | head -1 | awk '{$1=$2=""; print $0}' | sed 's/^[ \t]*//;s/[ \t]*$//' } get_win_info() { printf "Desktop environment: %s\n" ${XDG_CURRENT_DESKTOP} printf "Desktop resolution: %s\n" `xdpyinfo | grep -oP 'dimensions:\s+\K\S+'` } main() { funcs_to_test=( get_os_info get_bash_info get_kernel_version get_cpu_info get_win_info ) for testP in "${funcs_to_test[@]}" do $testP done } main "[email protected]" |
I love to find out stuff like this, this script prints useful information about your Linux system and it was easy to work out as well.
This is how to run a function automatically in a bash script, this is not hard at all.
main() { funcs_to_test=( get_os_info get_bash_info get_kernel_version get_cpu_info get_win_info ) for testP in "${funcs_to_test[@]}" do $testP done } main "[email protected]" |
And this is the output of this script.
┗━━━━━━━━━━┓ john@localhost ~/Documents ┗━━━━━━━━━━━━━╾ ╍▷ ./neo.sh ; echo $@ OS: Rocky Linux 9.2 (Blue Onyx) Bash version: 5.1.8 Kernel: 5.14.0-284.11.1.el9_2.x86_64 CPU: : Intel(R) Core(TM) i5-4670K CPU @ 3.40GHz Desktop environment: MATE Desktop resolution: 3840x1080 |
This really is a nice way to run a bash function, this is just like the main() function in C.