how to write shell scripts

锺博耘
2023-12-01

shell scripts

environment

Login shells read one or more startup files as shown below:

FileContent
/etc/profileA global configuration script that applies to all usres
~/.bash_profileA user’s personal startup file. Can be used to extend or override settings in the global configuration script.
~/.bash_loginIf ~/.bash_profile is not found, bash attempts to read this script.
~/.profileIf neither ~/.bash_profile nor ~/.bash_login is found, bash attempts to read this file. This is the default in Debian-based distributions, such as Ubuntu.

Non-login shell sessions read the following startup files:

FileContent
/etc/bash.bashrcA global configuration script that applies to all users.
~/.bashrcA user’s personal startup file. Can be used to extend or override settings in the global configuration script.

In addition to reading the startup files above, non-login shells also inherit the environment from their parent process, usually a login shell.

Variables

#!/bin/bash

title="This is a  title"

cat <<- EOF
        <html>
        <head>
            <title>
            $title
            </title>
        </head>
        <body>
        $title
        </body>
        </html>
EOF
How To Create A Variable

To create a variable, put a line in your script that contains the name of the variable followed immediately by an equal sign (“=”). No spaces are allowed. After the equal sign, assign the information you wish to store.

Shell Functions

Flow Control

test
ExpressionDescription
-d fileTrue if file is a directory
-e fileTrue if file exists
-f fileTrue if file exists and is a regular file
-L fileTrue if file is a symbolic link
-r fileTrue if file readable by you.
-w fileTrue if file writable by you.
-x fileTrue if file executable by you.
file1 -nt file2True if file1 is newer than(according to the modification)
file1 -ot file2True if ……..older ….
-z stringTrue if string is empty
-n stringTrue if string is not empty
str1 = str2True if str1 equals str2
str1 != str2………..not equal …
 类似资料:

相关阅读

相关文章

相关问答