Create a helloworld.sh file in the user's home directory (extension .sh)
Write #! / Bin / bash
on the first line.
This declares that the script will be interpreted and executed using a shell called bash!
!/bin/bash
Write the script on the second and subsequent lines.
echo "Hello World!"
In order to give execute permission to a file, first check the permission (permission) given to the file.
$ ls -l helloworld.sh
You can check the detailed information of the file by using the ls command + -l option + file name.
-Rw-r--r-- in the execution result is the permission of this file, and the 2nd-4th "rw-" from the left is the permission of the owner, so if you give the execution permission here, it's OK.
By the way, in this case, r and w indicate that read and write have read and write permissions, respectively. Change the permissions with the chmod command. (x is execute and execute permission)
$ chmod u+x helloworld.sh
Finally, specify the file path and execute the script!
$ ./helloworld.sh
This is Hello World!
--if statement
if condition; then
Processing when the condition is true
else
Processing when the condition is false
fi
--case statement
case string in
pattern 1
Processing when pattern 1 is matched
;;
Pattern 2
Processing when pattern 2 is matched
;;
Pattern 3
Processing when pattern 3 is matched
;;
esac
--for statement
for variable in word list
do
Repeated processing
done
--while statement
while command
do
Repeated processing
done
** Notes to remember when writing shell scripts ** https://qiita.com/piroor/items/77233173707a0baa6360
--Data is not passed as an argument, it is received from standard input by pipeline or redirect, and the processing result is output as standard output.
--It is best to receive data by standard input
--echo
if you want to process the string stored in the variable
--`` `cat``` if you want to process the contents of the file
-If you put a space before and after =, an error will occur.
--The shell script is a list of commands, and what kind of command is executed is considered =
――I saw a statement saying, "The grammar of the shell is premised on manual input. The shell script only executes it from above. So you can think that the shell script is also typing the command." Keep in mind.
I see.
https://shellscript.sunone.me/tutorial.html https://www.wakuwakubank.com/posts/347-linux-shell/
Recommended Posts