5 DERECHA
Linux2(Shell script) 본문
Basic Shell Script
A shell script is a program that is written in the shell language, which is used as the command-line interface for Linux and other Unix-like operating systems. Shell scripts are used to automate repetitive tasks, run commands in a specific sequence, or perform other tasks that require a series of commands to be executed in a specific order.
Here's an example of a simple shell script that prints "Hello, world!" to the console:
Let's break down what's happening in this script:
- The first line of the script (#!/bin/sh) is called a shebang, and tells the system which interpreter to use to execute the script. In this case, we're using the Bourne shell (sh), which is a common shell interpreter on Unix-like systems.
- The second line of the script (echo "Hello, world!") is a command that prints the text "Hello, world!" to the console.
To execute this script, we first need to make it executable by running the following command:
This command sets the executable permission on the file script.sh, which allows us to run it as a program. We can then execute the script by running:
This will run the script, which will print "Hello, world!" to the console.
Here's another example of a more complex shell script that uses variables and conditionals:
In this script, we're setting a variable called NAME to "John", and then checking if the variable is set using the -n flag in the conditional statement. If the variable is set, we print a message that says "Hello, John!". If the variable is not set, we print an error message.
To execute this script, we follow the same steps as before to make the script executable, and then run it using:
This will output "Hello, John!" to the console.
There are many other features and commands that can be used in shell scripts, such as loops, functions, and command-line arguments. Shell scripts are a powerful tool for automating tasks and performing complex operations on the command line. If you're interested in learning more, there are many online resources available, such as the Bash Scripting Guide and the Advanced Bash-Scripting Guide.
For loop
In shell scripting, a for loop is a control flow statement that allows you to iterate over a list of values and perform a set of actions for each value in the list.
Here's the basic syntax for a for loop in shell scripting:
Let's break down what's happening in this syntax:
- var is a variable that will hold each value in the list as we iterate over it.
- list is a list of values that we want to iterate over. This can be a list of files, directories, command-line arguments, or any other list of values that we want to process.
- The do keyword starts the block of code that will be executed for each value in the list.
- The done keyword ends the block of code that will be executed for each value in the list.
Here's an example of a for loop that iterates over a list of files and prints the name of each file to the console:
In this script, we're using a for loop to iterate over all of the .txt files in the current directory. For each file, we're using the echo command to print the name of the file to the console.
To run this script, we first need to make it executable using the chmod +x command, and then execute it using the ./script.sh command.
Here's another example of a for loop that iterates over a list of numbers and performs a mathematical calculation for each number:
In this script, we're using a for loop to iterate over a list of numbers from 1 to 5. For each number, we're adding it to the sum variable using the += operator. Finally, we're using the echo command to print the value of sum to the console.
To run this script, we follow the same steps as before to make it executable and execute it using the ./script.sh command. This will output "The sum of the numbers is: 15" to the console.
There are many other ways to use for loops in shell scripting, such as iterating over arrays or using the output of a command as the list of values to iterate over. For more information, you can refer to the Bash Scripting Guide or other online resources.
Parameter
In shell scripting, you can use parameters to pass arguments to your scripts at runtime. These parameters can be accessed within your script using special variables.
Here's an example of a shell script that accepts two parameters and uses them to perform a simple calculation:
In this script, we're using the $1 and $2 variables to access the first and second parameters that were passed to the script when it was executed. We're then using these parameters to perform a simple addition operation and output the result to the console using the echo command.
To run this script and pass in parameters, we would use the following command:
This would execute the script and pass in the values "5" and "10" as the first and second parameters, respectively. The output of the script would be:
In addition to $1 and $2, you can use $3, $4, and so on to access additional parameters that are passed to the script. You can also use the $# variable to determine how many parameters were passed, and the $0 variable to access the name of the script itself.
Here's an example of a script that uses a for loop and parameters to iterate over a list of files and perform a set of actions on each file:
In this script, we're using the $1 variable to access the first parameter that was passed to the script (which is assumed to be the directory to process). We're then using a for loop to iterate over all of the files in the directory, and performing some action on each file (in this case, printing the name of the file to the console).
To run this script and pass in a directory parameter, we would use a command like this:
This would execute the script and pass in the directory "/path/to/directory" as the first parameter. The script would then iterate over all of the files in the directory and perform the specified actions on each file.