[LINUX] Shell script @ study memo

What is a shell script?

Hello World! With a shell script!

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!

Shell script syntax

--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

in conclusion

** 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.

reference

https://shellscript.sunone.me/tutorial.html https://www.wakuwakubank.com/posts/347-linux-shell/

Recommended Posts

Shell script @ study memo
Shell memo
Shell script basics # 2
LPIC101 study memo
Bash script memo
Shell script command replacement
Job Scheduling @ Study Memo
DynamoDB Script Memo (Python)
Shell script special variables
Let's try a shell script
Basic syntax notes for shell script
Automate environment construction with Shell Script
Shell script numerical calculation bc command
[Ubuntu] How to execute a shell script
[Study memo] OAuth2 integration between django projects
Manage logrotate generations with a shell script.
Python & Machine Learning Study Memo ③: Neural Network
Study memo 1_eclipse (python) download, python execution, OpenPyXL download
Creating a shell script to write a diary
Python & Machine Learning Study Memo ④: Machine Learning by Backpropagation
Python & Machine Learning Study Memo ⑥: Number Recognition