How to Write A Shell Script to Read Files in Ubuntu 18.04?
Source: Internet
Author: User
Keywordsubuntu shell script ubuntu 18.04 create shell script how to do shell scripting in ubuntu
This article describes how to write a shell script to read files in Ubuntu 18.04. Demand function: read each line display from the file, count the total number of lines, and specify the file during the shell operation.
First, the shell experience 1. Open the terminal in the directory where you want to save the file and use vim to create a file named helloshell:
sudo vim helloshell
2. Enter the following code:
#!/bin/bash
echo "Hello World!"
The first line indicates that this is a shell script file, and the second line outputs "Hello World!", and then :wq saves and exits.
3. Modify file permissions (777 means all users can read, write and execute):
sudo chmod 777 helloshell
4. Run:
bash helloshell or ./helloshell
Two, read the file 1. Create test case file text
2. Create a shell script
3. Running results
Three. Number of output lines
1. Use awk'END{print NR}' [file name] to output the number of lines
2. Add this function to the shell script
3. Running results
Four, designated documents
The above shell file just writes the file name in the script, and has no practical meaning, so you need to write a command to specify the file to be read, just like vim [file name], refer to Vim, which is necessary for playing Linux Skills, one article to understand the installation and configuration of Vim, the method of learning.
"$1" represents the first parameter, which is the parameter immediately after read, which is passed in as the file name
In addition, echo -n "" is used here to print the number of lines without wrapping: to distinguish.
Five, global commands
Move the script to the directory corresponding to the environment variable (you can change /bin/xxx to determine the name of the run command, but be careful not to repeat):
sudo mv read /bin/read
Then add run permissions:
sudo chmod 755 /bin/read
final effect
The directory to open the terminal can be any location. You can run the shell script with the following command:
sudo read [file path]
In the same way, we can also write shell scripts for complex commands by ourselves to improve the efficiency of command usage, such as switching on the ss service, opening Ruijie client authentication, and so on.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.