[LINUX] Shells and shell scripts

Customize shell environment > Environment and shell variables

The user environment is defined by various variables (environment variables, shell variables). Environment variables are variables that are valid for the shell itself or for all processes launched from it. A shell variable is that shell. Only valid within. You can change shell variables to environment variables by exporting them with the *** export *** command, and with the *** env *** and *** printenv *** commands. You can display environment variables, shell variables and environment variables with the *** set *** command.

Shell options

Syntax: *** set [-o] [+ o] [option] *** Specify -o to enable option, + o to disable option

Alias

You can use the alias function (*** aliias *** command) to give an alias to a command, or to treat commands and options as a group like a new command. Use the *** unalias *** command.

Example) alias ls ='ls -l' However, even if you are using the alias function, you can execute it without options by typing *** \ ls *** etc.

Function definition

You can use the *** function *** command to define your own functions available on the bash shell.

Syntax: *** [function] {function name} () {command} *** function can be omitted. Half-width space is required in {}.

Use the *** declare -f *** command to display the defined function and the *** unset *** command to delete it.

bash config file

To set environment variables, aliases, and functions automatically when the shell is started, describe them in the configuration file.

-[] / etc / profile ... Executed at login. -[] /etc/bash.bashrc ... Executed when bash starts.

Shell script > Shell script basics

A shell script is a text file with commands written in it. The execution method is *** bash {script file name} ***, *** source {script file name} , ". **" * "," *** chmod a + x {script file name} *** ".

File check

Syntax: test {conditional statement} Conditional statement: Returns 0 if true, non-zero if false. -f {file} ... True if there is a file -d {directory} ... True if there is a directory Numerical value 1 -eq Numerical value 2 ... True if numerical value 1 and numerical value 2 are equal Character string 1 = Character string 2 ... True if character string 1 and character string 2 are equal

Control structure

Syntax 1: if conditional expression  then Execution statement 1  else Execution statement 2  fi

Syntax 2: case expression in Value 1) Execution statement 1 ;; Value 1) Execution statement 1 ;;  esac

Syntax 3: for variable name in list of values to assign to the variable  do Execution statement  done

  • *** A list of 10,11,12,13,14,15 is generated by using the *** seq *** command and typing'seq 10 15'.

Syntax 4: while conditional statement  do Execution statement  done

Shell script execution environment

To execute it, write *** #! / Bin / bash *** at the beginning of the script.

Recommended Posts

Shells and shell scripts
Using verticalenv in shell scripts
How to execute external shell scripts and commands in python
If FTP doesn't connect, check the user's login shell and / etc / shells
How to package and distribute Python scripts
Easily graph data in shell and Python