Linux 101: What does “sourcing a file” mean in Linux?

Jack Wallen explains the Linux supply command by the use of an instance.

linux-1.jpg

Picture: Larich/Shutterstock

Sourcing a file in Linux is an important idea, however it won’t be one you may use early on in your Linux profession. Even so, I will attempt to clarify this difficult idea in a means you’ll be able to perceive it. 

Sourcing a file makes it potential for an executable to “supply” info from a script as if the script had printed its output to the terminal. It isn’t a straightforward idea to understand, so I will present you by the use of an instance. 

SEE: 5 Linux server distributions try to be utilizing (TechRepublic Premium)

Let’s begin with a shell script, named sudocheck.sh that comprises a number of easy traces of code that warn any script that makes use of its output should be run with sudo privileges. That script would possibly look one thing like this:

check_root () {
  if [[ $EUID -ne 0 ]]; then
    echo "You need to run this script with sudo privileges"
    exit 1
  fi
}

Subsequent, we’ll create an executable file, named checksudo.sh, that may use the output of sudocheck.sh and print out if the execution has been completed with sudo privileges. That file would possibly look one thing like this:

#!/usr/bin/env bash
supply sudocheck.sh
check_root
echo "I've sudo privileges"

The checksudo.sh file will supply the sudocheck.sh file and use the ensuing output. 

Earlier than we truly execute the file, subject the command chmod u+x checksudio.sh. Now, subject the command ./checksudo.sh and you need to see printed “You need to run this script with sudo privileges.” 

Nonetheless, should you run the command sudo ./checksudo.sh you may see I’ve sudo privileges printed out. So the second script makes use of the output of the primary as a supply for enter. 

SEE: Rust: What builders must find out about this programming language (free PDF) (TechRepublic)

Through the use of this system, you’ll be able to create extra streamlined scripts that supply a number of information. You might additionally use the supply command in your scripts to learn configuration information, to save lots of house and time. 

And that is the supply command in a nutshell … or a bash shell, nonetheless, you need to have a look at it.

Additionally see

Recent Articles

spot_img

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here

Stay on op - Ge the daily news in your inbox