[LINUX] ShellScript memorandum

Introduction

Here, I would like to introduce the basic operation of shell script.

It also serves as a memorandum, so I would like to increase it little by little.

Basic

First from the basics

It seems that the first line is Shebang, which specifies the script. Hmm

test.sh


#!/bin/sh

#Comment out

echo "Hello, World!"

Change permissions

$ chmod 755 test.sh

Execution result

$ ./test.sh
Hello, World!

I want to have fun

Here, we are trying to enjoy the troublesome things when starting the application. Most things that can be done with a shell can be done

Example: MySQL Run normally for the time being

$ mysql -u [user name] -p

Enter password:

mysql> 

It is troublesome to enter the user name and password each time ...

First and simplest

sql.sh


#!/bin/sh
mysql -u [User name] -p[password]
#-Do not open a space next to p

You can run it with this, but with this you have to change that part sql.sh every time you change your username and password ... (I don't think it's too much trouble because it's only one line ...) So I'll just read it into another file.

Read from another file

test.txt


HELLO=hello

test.sh


#!/bin/sh

. ./test.txt
echo $HELLO

Run Don't forget to change your permissions! !!

$ ./test.sh 
hello

You can see that it is being read from another file.

Try to run

Let's actually load it separately and execute it

sql.cnf


USER=[User name]
PASS=[password]

It is not necessary to make it cnf separately, but it feels like entering from the shape

sql.sh


#!/bin/sh
. ./sql.cnf

mysql -u $USER -p$PASS

Execution result

$ ./sql.sh 

mysql> 

Summary

How was it? That's where I can think of it. Also, if I find or come up with an efficient method, I would like to write it.

Recommended Posts

ShellScript memorandum
Matplotlib memorandum
linux memorandum
jinja2 memorandum
Python memorandum
Django memorandum
Python Memorandum 2
plotly memorandum
Slackbot memorandum (1)
multiprocessing memorandum
Memorandum MetaTrader 5
[Linux/LPIC] Memorandum
pip memorandum
Python memorandum
pydoc memorandum
python memorandum
Pandas memorandum
python memorandum
DjangoGirls memorandum
Command memorandum
Python memorandum
pandas memorandum
python memorandum
Python memorandum
RAID type memorandum
Python pathlib memorandum
Knapsack problem memorandum
Memorandum of sed
Python memorandum (algorithm)
Linux memorandum [links]
nc command memorandum
Deep Learning Memorandum
numpy memorandum 1 / np.pad
Revit API memorandum
Memorandum conda command
Pandas operation memorandum
setuptools command memorandum
Python memorandum [links]
tslearn trial memorandum
Django's basic memorandum
Memorandum about validation