In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
How to use Shell script in Linux, for this question, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.
Sample Linux Shell script
Most shell scripts done on Linux involve bash shell. Advanced users with specified choices often use other Shell, such as Zsh and Ksh. Because of the widespread use and huge availability of the examples, we will stick to the Linux bash script for most of the examples. Our editor also tries to outline some examples of shell scripts that deal with shell other than bash. You will find that different Shell scripts are quite familiar with each other.
Linux Bash script
Bash, also known as Bourne Again Shell, is the default command line interpreter in most Linux distributions today. It is an upgrade to earlier Bourne Shell, and learning bash shell scripts will make you learn about other shell scripts faster. So try these simple examples for yourself to gain first-hand experience.
1. Hello World
Programmers often learn new languages by learning hello world programs. This is a simple program that prints the string "Hello World" to standard output. Create the file linuxidc.sh using an editor such as vim or nano and copy the following lines to it.
#! / bin/bash echo "Hello World,www.linuxidc.com"
Save and exit the file. You need to use the following command to make the file executable.
$chmod a + x linuxidc.sh
You can run it using either of the following two commands.
$bash linuxidc.sh $. / linuxidc.sh
It will print out the string passed to the internal echo of the script.
two。 Use echo for printing
The echo command is used to print information in bash format. It is similar to the C function 'printf', and provides many common options, including escape sequences and redirects.
Copy the following lines into a file called linuxidc.com.sh and make it executable, as described above.
#! / bin/bash echo "Linux Commune www.linuxidc.com" echo-n "print the text" echo-e "\ nUbuntu\ t CentOS\ t debian\ n" without line breaks
Run the script and see what it does. The-e option is used to tell echo that the string passed to it contains special characters and requires extended functionality.
3. Working with comments
Comments are useful for documentation and are a requirement for a high-quality code base. It is a common practice to put comments in code that deals with critical logic. To comment out a line, simply use the # (hash) character before it. Check the following bash script example.
#! / bin/bash # two values add up ((sum=17+19)) # print result echo $sum
The script will output the number 36. Please use # before some lines to check how to use comments. But the first line is an exception. It is called shebang and lets the system know which interpreter to use when running this script.
4. Multiline comment
Many people use multiline comments to record their Shell scripts. Check how this is done in the next script called linuxidc.com.sh.
#! / bin/bash: 'this script calculates the square of 5. '((area=5*5)) echo $area
Notice how the multiline comment is placed in the: 'and' character.
5. While cycle
The while loop structure is used to run certain instructions multiple times. Check out the following script called while.sh to better understand this concept.
#! / bin/bash iTun0 while [$I-le 2] do echo Number: $I ((iTunes +)) done
Therefore, the while loop takes the following form.
While [condition] do commands 1 commands n done
Square brackets are required.
6. For cycle
The for loop is another widely used bash shell construct that allows users to effectively traverse the code. Here is a simple example.
#! / bin/bash for ((axi1; a)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.