Because I have to use the machine to do stress testing before I get off work recently, I am more interested in shell scripts during this time. It is very convenient to write shell scripts with chatGPT.
The following are some cases
For example, I need to write a batch process: write a batch process to create 20 folders under the current folder, and each folder contains a text document. The first sentence of the text document is hello word. Save the following code as a create_folders.bat file, and run the batch file in the desired folder to complete the operation
.
@echo off
setlocal enabledelayedexpansion
for /l %%i in (1,1,20) do (
set folder=Folder_%%i
mkdir !folder!
echo hello world > !folder!\text.txt
)
echo 20 folders with text files have been created.
Isn’t it very convenient~~
If you want to rename the folder, for example: write a batch to rename all folders under the directory to 1.2.3.4, etc.
@echo off
setlocal enabledelayedexpansion
set count=1
for /d %%i in (*) do (
ren "%%i" "!count!"
set /a count+=1
)
echo All folders have been renamed.
Follow the steps just now, create a new txt file, copy it in, save it, rename it and run it
However, I usually write scripts with the .sh suffix under Linux. The differences between them are as follows:
.sh and .bat are the suffixes of script files in different operating systems, and they have the following differences:
-
.sh suffix: .sh is the suffix of Shell script files, used in Unix-like systems (such as Linux, Mac OS). A shell script is a text file that contains a series of shell commands for executing commands and controlling the flow. Typically Bash or other shell interpreters are used to execute .sh script files.
-
.bat suffix: .bat is the suffix of the batch script file, used in the Windows operating system. A batch script is a text file that contains a series of batch commands for executing commands and controlling the process. When running a .bat script file, the system will call the cmd.exe interpreter to execute the commands in it.
Below is a table comparing .sh and .bat script suffixes, including their syntax differences:
characteristic | .sh script | .bat script |
---|---|---|
suffix | .sh | .bat |
operating system | Unix-like operating system (such as Linux, Mac OS) | Windows operating system |
interpreter | Usually using Bash or other shell interpreter | Use cmd.exe interpreter |
comment symbol | # | REM |
variable declaration | use variable=value format |
use set variable=value format |
variable reference | use $variable or ${variable} method |
%variable% How to use |
Conditional statements | Use if, elif, else statement structure | Use if, else, goto and other statement structures |
loop statement | Use for, while, until and other loop structures | Use for, if, goto and other looping structures |
input Output | Use the echo command to output content, and use the read command to read user input | Use the echo command to output content, and use the set /p command to read user input |
array | Support array declaration and operation | Arrays are not supported |
file operation | Use cp, mv, rm and other commands to perform file operations | Use copy, move, del and other commands to perform file operations |
Pipelines and Redirection | Use the pipe symbol` | 和重定向符号 >、>>`等 |
function definition | function_name() { ... } define function using |
Define functions using :label andgoto label |
script execution permission | You need to add execute permission to the script file | No additional execute permissions are required |
To sum up, .sh and .bat are the extensions of different types of script files used in different operating systems. .sh is used for shell scripts in Unix systems, and .bat is used for batch scripts in Windows systems.