How to get the real path of the current script in Linux

How to get the real path of the current script in Linux

1. Get the real path of the current script:

#!/bin/bash
if [[ $0 =~ ^\/.* ]] #Judge whether the current script is an absolute path, matching all thens starting with /
  script=$0
else
  script=$(pwd)/$0
fi
script=`readlink -f $script` #Get the real path of the file script_path=${script%/*} #Get the directory where the file is located realpath=$(readlink -f $script_path) #Get the real path of the directory where the file is located echo $script
echo $script_path
echo $realpath 

2.readlink -f: Get the real file location of the symbolic link, and finally get the real file location of the symbolic link

3.basename: Get the file name of the file

4.dirname: Get the directory name of the file

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links

You may also be interested in:
  • How to enhance Linux and Unix server security
  • Detailed explanation of the difference between chown and chmod commands in Linux
  • Use the more, less, and cat commands in Linux to view file contents
  • Explanation of Linux kernel optimization configuration for high-concurrency nginx server
  • How to modify the default encoding of mysql in Linux
  • How to delete garbled or special character files in Linux
  • Introduction to the use and disabling of transparent huge pages in Linux
  • Detailed explanation of upgrading Python and installing pip under Linux
  • How to use for loop combined with if to find files in Linux Shell directory
  • Detailed explanation of using top command to analyze Linux system performance

<<:  React example showing file upload progress

>>:  Mysql delete duplicate data to keep the smallest id solution

Recommend

Two ways to reset the root password of MySQL database using lnmp

The first method: Use Junge's one-click scrip...

How to install mysql5.7.24 binary version on Centos 7 and how to solve it

MySQL binary installation method Download mysql h...

Linux/Mac MySQL forgotten password command line method to change the password

All prerequisites require root permissions 1. End...

How to rename the table in MySQL and what to pay attention to

Table of contents 1. Rename table method 2. Notes...

Summary of 7 reasons why Docker is not suitable for deploying databases

Docker has been very popular in the past two year...

Briefly describe the MySQL InnoDB storage engine

Preface: The storage engine is the core of the da...

Solve the problem of MySql8.0 checking transaction isolation level error

Table of contents MySql8.0 View transaction isola...

A brief talk about JavaScript variable promotion

Table of contents Preface 1. What variables are p...

How to install and persist the postgresql database in docker

Skip the Docker installation steps 1. Pull the po...

How to get datetime data in mysql, followed by .0

The data type of MySQL is datetime. The data stor...

Detailed explanation of HTML basics (Part 2)

1. List The list ul container is loaded with a fo...

Vue network request scheme native network request and js network request library

1. Native network request 1. XMLHttpRequest (w3c ...

How to use CocosCreator object pool

Table of contents Preface: Specific operations St...