We need to learn to use tools to free hands, such as batch to folder some files to create soft links, we can write a script to achieve. Here are some of the commands used in this project.

variable

When a variable is defined, the variable name is not marked with a dollar sign

your_name="12"

Use the variable

To use a defined variable, simply precede the variable name with a dollar sign

your_name="qinjx"
echo $your_name
echo ${your_name}

Braces around the variable name are optional or not, and are added to help the interpreter recognize the bounds of the variable

Gets the name of a folder/file in a directory

  1. If you need deep traversal, that is, the output folder and the files/folders in the folder, the command is as follows
#! /bin/bash CD for file in $(ls *) do echo $file done
  1. If you only want to get the first layer of files and folders, then look like this
#! /bin/bash CD for file in $(ls) do echo $file done

Judgment of folders and files

The first syntax to be evaluated is the if [condition] two commands:

  1. -f “file”: determine if file is a file;
  2. -d “file”: Check if file is a directory.

Combined with the syntax for getting files/folders, such as whether it is a folder, you can write this

#! /bin/bash CD for file in $(ls) do if [-d "$file"]; then echo "$file is a directory " elif [ -f "$file" ]; then echo "$file is a file" fi done

Whether the array contains a value

We know that JavaScript includes can be used directly with [].includes(‘ XXX ‘). With a shell, you can write:

if [[ " ${array[@]} " =~ " ${value} " ]]; then
  echo true
fi
if [[ ! " ${array[@]} " =~ " ${value} " ]]; then
  echo false
fi

Process control

If statement

if condition
then
    command1 
    command2
    ...
    commandN 
fi

if else

if condition 
then
    command1 
    command2
else 
    command
fi

if else-if else

if condition1
then
    command1
elif condition2 
then 
    command2
else
    commandN
fi

Gets the absolute path to a file

  1. The absolute path to get the file is available
$(pwd)
  1. If you want to get the absolute path to a relative file, you can write this
$(cd ${basePath}; pwd)