Linux, commands, shells ... I've overcome that kind of bitterness! !! In this article, I will introduce the sites that I have referred to and supplement my questions and interpretations. I hope this article will help those who think "I wonder if I should try it" or "I'm scared of the black screen".
This is explained in various places, but in a nutshell, in my interpretation, it is a ** program that puts together instructions to a PC **.
For example, when you want to rename a file. When you want to unify the fluctuation of the wording of "test" and "test" of multiple file names, specify the file or directory and rename it at once. And.
As another example, I want to create a log file of executed commands every day (it seems to be logging). And. ... Well, isn't that about batch processing? ?? I thought, and investigated. → https://wa3.i-3-i.info/word11221.html Apparently, shell scripts and batch files have the same role, only the base world is different.
MacOS can be executed from the terminal without environment construction, but Windows requires preparation. Try google with "Windows Linux" etc .: bow_tone1:
I referred to the following script creation method and execution method ↓ https://udemy.benesse.co.jp/development/web/shellscript.html
The editor uses ** Vi **, but you should be able to use almost anything with a text editor. Recently, the evolved version of Vi, ** Vim **, seems to be popular. You can color-code the code by setting Vi and Vim, but I will omit it this time. VS Code automatically color-codes it and is easy to see. friendly.
All files and folders have permissions.
It can be set to prevent accidents such as accidentally running the shell or making it impossible to edit the production source or configuration file.
You can check the permissions with ls -l
.
$ ls -l
total 13784
drwxr-xr-x 4 aaaaaayyymmm staff 128 3 20 15:19 memo
drwxr-xr-x 7 aaaaaayyymmm staff 224 3 29 23:52 tmp
-rw------- 1 aaaaaayyymmm staff 124987 3 30 11:51 Archive.zip
-rw-r--r-- 1 aaaaaayyymmm staff 197519 3 31 16:00 screenshot.png
-rw-r--r-- 1 aaaaaayyymmm staff 17 3 31 16:55 hello.sh
The meaning of each item is as follows.
File type | Authority | Number of hard links | owner | group | Byte size | Time stamp | file name |
---|---|---|---|---|---|---|---|
-(First character part) | rw-r--r-- | 1 | aaaaaayyymmm | staff | 17 | 3 31 16:55 | hello.sh |
This time, we'll focus on ** file types ** and ** permissions **. There are three types of files as follows.
type | meaning | Supplement |
---|---|---|
- | File | --- |
d | directory | The same thing as the folder you see in the GUI |
l | Symbolic link | Things like shortcuts. Proxy files that can read and write real files and directories |
The view of authority shows the authority of ** owner **, ** group **, ** other ** users, separated by 3 from the left. And the types of authority are as follows.
Authority | Numeric notation | meaning |
---|---|---|
r | 4 | reading |
w | 2 | Edit |
x | 1 | Run |
- | None | No authority |
There are two main ways to write.
Number designation ver.Example
$ chmod 744 hello.sh
Alphabet designation ver.Example
$ chmod g+wx hello.sh
//Or
$ chmod u=x,g=rwx,o=- hello.sh
I referred to this article. If you want to know more details, please refer to the following. https://qiita.com/shisama/items/5f4c4fa768642aad9e06
The default is -rw-r--r--
.
To execute the script after it is created, you need to grant the execution right as follows.
(The default authority can be changed, but this time it is not handled.)
$ chmod u+x hello.sh
I learned the following ↓ http://motw.mods.jp/shellscript/tutorial.html
I understand -eq, but I want to use -gt and -le quickly, but it's hard to remember. .. .. Knowing English makes it a little easier to remember!
operator | English | meaning |
---|---|---|
eq | equal | x = y |
ge | greater than or equal to | x >= y |
gt | greater than | x > y |
le | less than or equal to | x <= y |
lt | less than | x < y |
ne | not equal | x != y |
I referred to the following site http://motw.mods.jp/shellscript/rename.html
I thought about this myself and created it.
make_bk.sh
#!/bin/bash
read -p "Enter the file name or folder name you want to back up:" ary
echo $ary
#If there is no input value, an error message will be displayed and the process will end.
if [ -z $ary ];
then
echo "Enter the file name or folder name you want to back up"
exit 1
fi
#Create a backup folder.-With the p option, a new folder is created only when the folder does not exist.
bkdir=bk_$(date +%Y%m%d)
mkdir -p $bkdir
#Read the entered files or folders one by one
for target in ${ary[@]}
do
#Make a copy and store it in a backup folder
[ -d $target ] && cp -r $target $bkdir/$target || cp $target $bkdir/$target
done
This script doesn't limit the number of files or folders, so I used an array when accepting input. For how to use the array, refer to the following. https://www.atmarkit.co.jp/ait/articles/1905/22/news004.html
About the following parts.
[ -d $target ] && cp -r $target $bkdir/$target || cp $target $bkdir/$target
With the cp command, the "-r option" is required when copying a folder (= directory), so the cp command is executed after determining whether $ target is a directory. I would be grateful if you could tell me if there is another good way.
Before that, give the execution right.
$ chmod u+x make_bk.sh
Folder structure before execution
tmp
├── make_bk.sh
└── test.txt //Copy this guy
I will do it
$ make_bk.sh
Enter the file name or folder name you want to back up:test.txt
test.txt
Folder structure after execution
tmp
├── bk_20200329 //Newly created folder
│ └── test.txt //Copyed file
├── make_bk.sh
└── test.txt
:blush:
next, Copy & backup for each folder If a folder exists, just copy the target Make sure.
Folder structure before execution
tmp
├── bk_20200329
│ └── test.txt
├── make_bk.sh
├── test.txt
└── test2 //Copy this guy
└── test2.txt //Should be copied at the same time
I will do it
$ make_bk.sh
Enter the file name or folder name you want to back up:test2
test2
Folder structure after execution
tmp
├── bk_20200329
│ ├── test.txt
│ └── test2 //Copyed folder
│ └── test2.txt //Files copied together
├── make_bk.sh
├── test.txt
└── test2
└── test2.txt
:blush::blush::blush:
This is a memo of the command that I thought "it's convenient and seems to be used" while learning. ◆ I want to display the date and time when executing the history command Reference: https://linuxfan.info/add-datetime-to-history ◆ I want to record a work log Reference: https://dev.classmethod.jp/articles/scriptcommand/
You should have grasped the basics! I'll try everything. The purpose of this time was to get used to the script, so I can't say that the practicality of the created script is questioned. As I work from now on, I feel like I can make it when I think "I want to automate this" and "I can put together this command !?", so I'm looking forward to the time coming ... As the next step, we will study so that we can pay attention to execution speed and reusability ...!
Recommended Posts