您的位置:首页 > 娱乐 > 明星 > 免费公司网站申请_免费ppt模板下载网址不需要会员_百度指数明星人气榜_软文媒体

免费公司网站申请_免费ppt模板下载网址不需要会员_百度指数明星人气榜_软文媒体

2025/5/6 8:23:35 来源:https://blog.csdn.net/weixin_44444492/article/details/143512517  浏览:    关键词:免费公司网站申请_免费ppt模板下载网址不需要会员_百度指数明星人气榜_软文媒体
免费公司网站申请_免费ppt模板下载网址不需要会员_百度指数明星人气榜_软文媒体

Bash Script Tutorial

1. Script Basics

1.1 Creating a Bash Script

  1. Create a script file with a .sh extension:
    touch myscript.sh
    
  2. Make the script executable:
    chmod +x myscript.sh
    
  3. Add the shebang line at the top to specify the interpreter:
    #!/bin/bash
    

1.2 Running a Bash Script

Run the script from the terminal by providing the path:

./myscript.sh

2. Variables

2.1 Defining Variables

Variables do not need a data type declaration in Bash:

name="John"
age=25

2.2 Accessing Variables

Use the $ symbol to access a variable’s value:

echo "My name is $name and I am $age years old."

2.3 Reading User Input

You can use the read command to get input from the user:

echo "Enter your name:"
read user_name
echo "Hello, $user_name!"

3. Conditionals

3.1 if Statements

if [ condition ]; then# Code to execute if the condition is true
fi

3.2 if-else Statements

if [ condition ]; thenecho "Condition is true"
elseecho "Condition is false"
fi

3.3 if-elif-else Example

if [ $age -lt 18 ]; thenecho "You are a minor."
elif [ $age -ge 18 ] && [ $age -lt 65 ]; thenecho "You are an adult."
elseecho "You are a senior."
fi

3.4 File Conditionals

Check if a file or directory exists:

if [ -f "/path/to/file" ]; thenecho "File exists."
fiif [ -d "/path/to/directory" ]; thenecho "Directory exists."
fi

3.5 String Comparison

str1="hello"
str2="world"if [ "$str1" == "$str2" ]; thenecho "Strings are equal."
elseecho "Strings are not equal."
fi

4. Loops

4.1 for Loop

for i in 1 2 3; doecho $i
done

4.2 for Loop with Range

for i in {1..5}; doecho $i
done

4.3 while Loop

count=1
while [ $count -le 5 ]; doecho $count((count++))
done

4.4 until Loop

count=1
until [ $count -gt 5 ]; doecho $count((count++))
done

5. Arrays

5.1 Declaring Arrays

fruits=("apple" "banana" "cherry")

5.2 Accessing Array Elements

echo ${fruits[0]}  # Output: apple

5.3 Looping Through an Array

for fruit in "${fruits[@]}"; doecho $fruit
done

5.4 Array Length

echo ${#fruits[@]}  # Output: 3

6. Functions

6.1 Defining a Function

function greet() {echo "Hello, $1!"
}

6.2 Calling a Function

greet "Alice"  # Output: Hello, Alice!

6.3 Returning Values

Bash functions return values using return or output:

function add() {local sum=$(( $1 + $2 ))echo $sum
}result=$(add 10 20)
echo "Sum: $result"  # Output: Sum: 30

7. File and Directory Operations

7.1 Creating Directories

mkdir myfolder

7.2 Checking if a Directory Exists

if [ ! -d "myfolder" ]; thenmkdir myfolder
fi

7.3 Listing Files in a Directory

for file in *; doecho $file
done

8. Input/Output Redirection

8.1 Redirecting Output to a File

echo "Hello, World!" > output.txt  # Overwrites the file
echo "Append this line" >> output.txt  # Appends to the file

8.2 Redirecting Input from a File

while read line; doecho $line
done < input.txt

8.3 Redirecting Error Output

command 2> error.log  # Redirect stderr to error.log
command > output.log 2>&1  # Redirect stdout and stderr to output.log

9. Special Variables

9.1 Positional Parameters

#!/bin/bash
echo "Script name: $0"
echo "First argument: $1"
echo "Second argument: $2"

9.2 Script Exit Status

command
if [ $? -eq 0 ]; thenecho "Command succeeded."
elseecho "Command failed."
fi

9.3 Looping Through Arguments

for arg in "$@"; doecho "Argument: $arg"
done

10. Miscellaneous

10.1 Exit Script with Status Code

if [ "$user" != "admin" ]; thenecho "Access denied."exit 1
fi

10.2 Sleeping for a Few Seconds

sleep 5  # Waits for 5 seconds

10.3 Commenting in Bash

Use # for single-line comments:

# This is a comment
echo "This is code."

11. Combining Everything in a Complete Script Example

Here’s a complete example script that demonstrates many of these concepts:

#!/bin/bash# Function to create a directory if it doesn't exist
create_directory() {if [ ! -d "$1" ]; thenmkdir "$1"echo "Directory '$1' created."elseecho "Directory '$1' already exists."fi
}# Greet the user
echo "Enter your name:"
read user_name
echo "Hello, $user_name!"# Create directories
projects=("project1" "project2" "project3")
for project in "${projects[@]}"; docreate_directory "$project"
done# Example of a conditional
if [ "$user_name" == "admin" ]; thenecho "Welcome, admin!"
elseecho "You are not the admin."
fi# Example of a loop
for i in {1..5}; doecho "Iteration $i"
done# Function with return value
function add() {local result=$(( $1 + $2 ))echo $result
}sum=$(add 10 20)
echo "The sum of 10 and 20 is: $sum"

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com