Wed. Oct 27th, 2021


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

linux-1.jpg

Picture: Larich/Shutterstock

Sourcing a file in Linux is a vital idea, but it surely won’t be one you will use early on in your Linux profession. Even so, I will attempt to clarify this difficult idea in a method you’ll be able to perceive it. 

Sourcing a file makes it doable for an executable to “supply” data from a script as if the script had printed its output to the terminal. It is not a straightforward idea to understand, so I will present you by means of an instance. 

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

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

check_root () {
  if [[ $EUID -ne 0 ]]; then
    echo "You will 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 carried out with sudo privileges. That file may 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, problem the command chmod u+x checksudio.sh. Now, problem the command ./checksudo.sh and it’s best to see printed “You will need to run this script with sudo privileges.” 

Nonetheless, in the event you run the command sudo ./checksudo.sh you will 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 have to find out about this programming language (free PDF) (TechRepublic)

By utilizing this machine, you’ll be able to create extra streamlined scripts that supply a number of recordsdata. You could possibly additionally use the supply command in your scripts to learn configuration recordsdata, to avoid wasting area and time. 

And that is the supply command in a nutshell … or a bash shell, nevertheless, you wish to take a look at it.

Additionally see



Source link

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *