[LINUX] Let's try a shell script

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

What is a shell script?

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.

Script creation method and execution method

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.

About authority

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

How to grant 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

Hit the basics of shell scripts

I learned the following ↓ http://motw.mods.jp/shellscript/tutorial.html

How to remember comparison operators

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

Let's make a practical script

Part 1: Rename all file names at once

I referred to the following site http://motw.mods.jp/shellscript/rename.html

Part 2: Copy files and folders to backup folder

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.

Let's run

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:

Bonus: Convenient Linux commands

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/

in conclusion

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

Let's try a shell script
Try programming with a shell!
Shell script basics # 2
[Ubuntu] How to execute a shell script
Manage logrotate generations with a shell script.
Creating a shell script to write a diary
Let's run a Bash script in Java
View today's weather forecast with a shell script
Launch a shell while a Python script is running
Try to write a ping confirmation script appropriately
Try creating a FizzBuzz problem with a shell program
Shell script command replacement
Shell script special variables
Shell script @ study memo
A shell script that puts Webmin into Alpine Linux
[Piyopiyokai # 1] Let's play with Lambda: Creating a Python script
Let's understand the standard input / output of bash together and write a shell script.
A shell script that numbers duplicate names when creating files
How to run a Python program from within a shell script
[Python, shell script, team development] Create a nifty Git repository
[Linux] Copy data from Linux to Windows with a shell script
Let's create a script that registers with Ideone.com in Python.
Try creating a CRUD function
Process the files in the folder in order with a shell script
Let's draw a logistic function
I'll never forget how to write a shell script, don't forget! !!
[Linux] Write a deployment tool using rsync with a shell script
Let's make a rock-paper-scissors game
Try to select a language
Process the contents of the file in order with a shell script
Let's try analysis! Chapter 1: Utilization of Analytical Technology-From a Business Perspective-
Conditional branch due to the existence of a shell script file
Create a shell script to run the python file multiple times
How to determine if a shell script was started in bash
Run a multi-line script in a PDB
Let's make a remote rumba [Hardware]
[Infrastructure] Linux command, shell script collection
Basic syntax notes for shell script
Try to draw a Bezier curve
Try throwing a query in Redash
I made a CUI-based translation script (2)
Let's make a GUI with python.
Let's make a spot sale service 2
Let's make a breakout with wxPython
Maybe it works! Let's try scraping!
Let's make a spot sale service 1
Let's try Fizz Buzz in Python
A little script for malware self-defense
Automate environment construction with Shell Script
Let's make a graph with python! !!
Let's make a supercomputer with xCAT
Launch a Python script as a service
Let's make a spot sale service 3
Shell script numerical calculation bc command
Write a batch script with Python3.5 ~
Wow Pandas Let's learn a lot
A shell script to make sure you don't forget the pipenv shell again