Home  >  Article  >  Operation and Maintenance  >  How to execute files in linux

How to execute files in linux

藏色散人
藏色散人Original
2019-11-13 10:03:0512390browse

How to execute files in linux

How to execute .sh file in linux

How to execute .sh file in linux

1. The .sh file is a text file. If you want to execute it, you need to use chmod a x xxx.sh to give executable permissions.

Is it a bash script?

You can use touch test.sh #Create the test.sh file

vi test.sh #Edit the test.sh file

Add content

#! /bin/bash
mkdir test

Save and exit.

chmod a x test.sh #Give test.sh executable permissions

For example, test,sh file is under the /home/work file

Method 1 Run in your own directory

Enter the cd /home/workwen file

Execute ./test.sh

The command will be in the current directory Create a "test" directory.

Method 2 Run with absolute strength

Execute /home/work/test.sh

Method 3 Run in your own directory

sh test.sh

Final suggestion: use

C code

1.man sh

man sh Let’s take a look at the introduction of sh~

Recommended: "

Linux Tutorial"

linux.sh syntax

Introduction:

1 Beginning

The program must start with the following line (must be placed on the first line of the file):

# !/bin/sh

The symbol #! is used to tell the system that the parameters behind it are the programs used to execute the file. In this example we use /bin/sh to execute the program.

When writing the script is complete, if you want to execute the script, you must also make it executable.

To make the script executable:

Compile chmod x filename so that you can use ./filename to run

2 Notes

When doing shell programming , sentences starting with # represent comments until the end of the line. We sincerely recommend using comments in your programs.

If you use comments, you can understand the function and working principle of the script in a short time even if you have not used the script for a long time.

3 Variables

In other programming languages ​​you have to use variables. In shell programming, all variables are composed of strings, and you do not need to declare variables. To assign a value to a variable, you can write:

#!/bin/sh

#Assign a value to a variable:

a=”hello world”

# Now print the content of variable a:

echo “A is:”

echo $a

Sometimes variable names are easily confused with other words, such as:

num=2

echo “this is the $numnd”

This does not print out “this is the 2nd”, but only prints “this is the “, Because the shell will search for the value of the variable numnd, but this variable has no value. You can use curly braces to tell the shell that what we want to print is the num variable:

num=2

echo “this is the ${num}nd”

This will print : this is the 2nd

4 Environment variable

The variables processed by the export keyword are called environment variables. We will not discuss environment variables because typically they are only used in login scripts.

5 Shell commands and process control

Three types of commands can be used in shell scripts:

1)Unix commands:

Although in shell scripts You can use any unix command, but there are some relatively more commonly used commands. These commands are usually used for file and text operations.

Common command syntax and functions

echo “some text”: print the text content on the screen

ls: file list

wc –l file wc -w file wc -c file: Count the number of lines in the file; count the number of words in the file; count the number of characters in the file

cp sourcefile destfile: file copy

mv oldname newname: Repeat Name the file or move the file

rm file: Delete the file

grep 'pattern' file: Search for a string in the file For example: grep 'searchstring' file.txt

cut -b colnum file: Specify the range of file contents to be displayed and output them to the standard output device. For example: output the 5th to 9th characters of each line cut -b5-9 file.txt. Do not confuse it with the cat command.

These are two completely different commands

cat file.txt: Output the file content to the standard output device (screen)

file somefile: Get the file type

read var: Prompt the user for input and assign the input to the variable

sort file.txt: Sort the lines in the file.txt file

uniq: Delete the text file Duplicate lines that appear, such as: sort file.txt | uniq

expr: perform mathematical operations Example: add 2 and 3 is expr 2 “ ” 3

find: search for files, for example: based on files Name search find . -name filename -print

tee: Output data to the standard output device (screen) and file such as: somecommand | tee outfile

basename file: Return the file name without a path. For example: basename /bin/tux will return tux

dirname file: Returns the path of the file. For example: dirname /bin/tux will return /bin

head file: prints the first few lines of the text file

tail file: prints the text file The last few lines

sed: Sed is a basic search and replace program. You can read text from standard input (such as a command pipe) and output

results to standard output (screen). This command uses regular expressions (see Reference) to search. Not to be confused with wildcards in the shell. For example: replace linuxfocus with LinuxFocus: cat text.file | sed ’s/linuxfocus/LinuxFocus/’ >newtext.file

awk: awk is used to extract fields from text files. By default, the field separator is a space. You can use -F to specify other separators.

cat file.txt | awk -F, ‘{print $1 “,” $3}’ Here we use as the field separator to print the first and third fields at the same time. If the content of the file is as follows: Adam Bor, 34, IndiaKerryMiller, 22, USA, the command output is: Adam Bor, IndiaKerry Miller, USA

2) Concept: pipeline, redirection and backtick

These are not system commands, but they are really important.

Pipe (|) uses the output of one command as the input of another command.

grep "hello" file.txt | wc -l

Search for lines containing "hello" in file.txt and count the number of lines.

Here the output of the grep command is used as the input of the wc command. Of course you can use multiple commands.

Redirection: Output the results of the command to a file instead of the standard output (screen).

> Write to the file and overwrite the old file

>> Append to the end of the file, retaining the old file content.

Backslash

Use the backslash to use the output of one command as a command line parameter of another command.

Command:

find . -mtime -1 -type f -print

is used to find modifications in the past 24 hours (-mtime –2 means the past 48 hours) passed files. If you want to package all the found files, you can use the following linux script:

#!/bin/sh

# The ticks are backticks (`) not normal quotes ( '):

tar -zcvf lastmod.tar.gz `find . -mtime -1 -type f -print`

3) Process control

1.if

The “if” expression executes the part after then if the condition is true:

if ….; then

….

elif ….; then

….

else

….

fi

In most cases, you can use test commands to test conditions test. For example, you can compare strings, determine whether a file exists and is readable, etc...

Usually "[ ]" is used to represent conditional testing. Note that the spaces here are important. Make sure there are spaces between square brackets.

[ -f "somefile" ] : Determine whether it is a file

[ -x "/bin/ls" ] : Determine whether /bin/ls exists and has executable permissions

[ -n "$var" ] : Determine whether the $var variable has a value

[ "$a" = "$b" ] : Determine whether $a and $b are equal

Execute man test to view all types that can be compared and judged by test expressions.

Directly execute the following script:

#!/bin/sh

if [ "$SHELL" = "/bin/bash" ]; then

echo “your login shell is the bash (bourne again shell)”

else

echo “your login shell is not bash but $SHELL”

fi

The variable $SHELL contains the name of the login shell, which we compared with /bin/bash.

Shortcut Operators

Friends who are familiar with C language may like the following expression:

[ -f "/etc/shadow" ] && echo “This computer uses shadow passwords”

Here && is a shortcut operator. If the expression on the left is true, the statement on the right will be executed.

You can also think of it as the AND operation in logical operations. The above example means that if the /etc/shadow file exists, print "This computer uses shadow passwords". The same OR operation (||) is also available in shell programming. Here is an example:

#!/bin/sh

mailfolder=/var/spool/mail/james

[ -r "$mailfolder" ]' '{ echo “Can not read $mailfolder” ; exit 1; }

echo “$mailfolder has mail from:”

grep “^From ” $mailfolder

The script first Determine whether the mailfolder is readable. If readable, print the "From" line in the file. If it is not readable, the OR operation takes effect, and the script exits after printing an error message. There is a problem here, that is, we must have two commands:

◆Print error message

◆Exit the program

We use curly braces to put two commands together as one command in the form of anonymous functions. General functions are mentioned below.

We can also use if expressions to do anything without the AND or operators, but it is much more convenient to use the AND or operators.

The above is the detailed content of How to execute files in linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn