Other

[bash] source / export

2018-12-25  本文已影响0人  何幻

1. source

source, . (dot command)

This command, when invoked from the command-line, executes a script.
Within a script, a source file-name loads the file file-name.

Sourcing a file (dot-command) imports code into the script, appending to the script (same effect as the #include directive in a C program).
The net result is the same as if the "sourced" lines of code were physically present in the body of the script.

This is useful in situations when multiple scripts use a common data file or function library.

Example:

#!/bin/bash # Note that this example must be invoked with bash, i.e., bash ex38.sh 
#+ not sh ex38.sh !

. data-file # Load a data file.
# Same effect as "source data-file", but more portable.

# The file "data-file" must be present in current working directory, 
#+ since it is referred to by its basename.

# Now, let's reference some data from that file.

echo "variable1 (from data-file) = $variable1" 
echo "variable3 (from data-file) = $variable3"

let "sum = $variable2 + $variable4" 
echo "Sum of variable2 + variable4 (from data-file) = $sum" 
echo "message1 (from data-file) is \"$message1\"" 
# Escaped quotes 
echo "message2 (from data-file) is \"$message2\""

print_message This is the message-print function in the data-file.

exit $?

File data-file above. Must be present in same directory.

# This is a data file loaded by a script.
# Files of this type may contain variables, functions, etc.
# It loads with a 'source' or '.' command from a shell script.

# Let's initialize some variables.

variable1=23 
variable2=474 
variable3=5 
variable4=97

message1="Greetings from *** line $LINENO *** of the data file!" 
message2="Enough for now. Goodbye."

print_message () 
{            # Echoes any message passed to it.

  if [ -z "$1" ] 
  then 
    return 1 # Error, if argument missing. 
  fi

  echo

  until [ -z "$1" ] 
  do             # Step through arguments passed to function.
    echo -n "$1" # Echo args one at a time, suppressing line feeds.
    echo -n " "  # Insert spaces between words.
    shift        # Next one. 
  done

  echo

  return 0
}

Arguments may be (optionally) passed to the sourced file as positional parameters.

source $filename $arg1 arg

It is even possible for a script to source itself, though this does not seem to have any practical applications.

2. export

export
The export command makes available variables to all child processes of the running script or shell.

One important use of the export command is in startup files, to initialize and make accessible environmental variables to subsequent user processes.

Unfortunately, there is no way to export variables back to the parent process, to the process that called or invoked the script or shell.

Example:
Using export to pass a variable to an embedded awk script.

#!/bin/bash

# Yet another version of the "column totaler" script (col-totaler.sh) 
#+ that adds up a specified column (of numbers) in the target file.
# This uses the environment to pass a script variable to 'awk' . . . 
#+ and places the awk script in a variable.

ARGS=2 
E_WRONGARGS=85

if [ $# -ne "$ARGS" ] # Check for proper number of command-line args. 
then
  echo "Usage: `basename $0` filename column-number"
  exit $E_WRONGARGS 
fi

filename=$1 
column_number=$2

#===== Same as original script, up to this point =====#

export column_number 
# Export column number to environment, so it's available for retrieval.

# -----------------------------------------------
awkscript='{ total += $ENVIRON["column_number"] } 
END { print total }' 
# Yes, a variable can hold an awk script.
# -----------------------------------------------

# Now, run the awk script. 
awk "$awkscript" "$filename"

# Thanks, Stephane Chazelas.

exit 0

Reference

Advanced Bash-Scripting Guide

上一篇 下一篇

猜你喜欢

热点阅读