Linux 101: What does "sourcing a file" mean in Linux?

3 years ago 270

Jack Wallen explains the Linux root bid by mode of an example.

linux-1.jpg

Image: Larich/Shutterstock

Sourcing a record successful Linux is simply a precise important concept, but it mightiness not beryllium 1 you'll usage aboriginal connected successful your Linux career. Even so, I'm going to effort to explicate this challenging conception successful a mode you tin recognize it. 

Sourcing a record makes it imaginable for an executable to "source" accusation from a publication arsenic though the publication had printed its output to the terminal. It's not an casual conception to grasp, truthful I'm going to amusement you by mode of an example. 

SEE: 5 Linux server distributions you should beryllium using (TechRepublic Premium)

Let's commencement with a ammunition script, named sudocheck.sh that contains a fewer elemental lines of codification that pass immoderate publication that uses its output indispensable beryllium tally with sudo privileges. That publication mightiness look thing similar this:

check_root () { if [[ $EUID -ne 0 ]]; then echo "You indispensable tally this publication with sudo privileges" exit 1 fi }

Next, we'll make an executable file, named checksudo.sh, that volition usage the output of sudocheck.sh and people retired if the execution has been done with sudo privileges. That record mightiness look thing similar this:

#!/usr/bin/env bash source sudocheck.sh check_root echo "I person sudo privileges"

The checksudo.sh record volition root the sudocheck.sh record and usage the resulting output. 

Before we really execute the file, contented the bid chmod u+x checksudio.sh. Now, contented the bid ./checksudo.sh and you should spot printed "You indispensable tally this publication with sudo privileges." 

However, if you tally the bid sudo ./checksudo.sh you'll spot I person sudo privileges printed out. So the 2nd publication uses the output of the archetypal arsenic a root for input. 

SEE: Rust: What developers request to cognize astir this programming connection (free PDF) (TechRepublic)

By utilizing this device, you tin make much streamlined scripts that root aggregate files. You could besides usage the root bid successful your scripts to work configuration files, to prevention abstraction and time. 

And that's the root bid successful a nutshell ... oregon a bash shell, however, you privation to look astatine it.

Open Source Weekly Newsletter

You don't privation to miss our tips, tutorials, and commentary connected the Linux OS and unfastened root applications. Delivered Tuesdays

Sign up today

Also spot

Read Entire Article