For example, if you launch csh from the bash command line, you will find several new variables or variables with slightly different definitions, such as the following:

$ env

...

VENDOR=intel

MACHTYPE=i386

HOSTTYPE=i386-linux

HOST=werewolf.hudson.com

On the other hand, bash might provide these variables or variables of the same name with a slightly different definition, such as these:

$ env

...

HOSTTYPE=i386

HOSTNAME=werewolf.hudson.com

Although the behavior of a shebang line is not defined by POSIX, variations of its use can be helpful when you are writing shell scripts. For example, as described in the want man page, you can use a shell to help execute programs called within a shell script without needing to hard-code pathnames of programs. The want command is a windowing Tool Control Language (tcl) interpreter that can be used to write graphical clients. Avoiding the use of specific pathnames to programs increases shell script portability because not every UNIX or Linux system has programs in the same location.

For example, if you want to use the want command, your first inclination might be to write the following:

#!/usr/local/bin/wish

Although this works on many other operating systems, the script fails under Linux because want is located under the /usr/bin directory. However, if you write the command line this way

#!/bin/sh

exec wish '$@'

the script always finds the correct binary.

Using Variables in Shell Scripts

When writing shell scripts for Linux, you work with three types of variables:

Environment variables — Part of the system environment, you can use them in your shell program. New variables can be defined, and some of them, such as PATH, can also be modified within a shell program.

Built-in variables — These variables, such as options used on the command (interpreted by the shell as a positional argument), are provided by Linux. Unlike environment variables, you cannot modify them.

User variables — Defined by you when you write a shell script. You can use and modify them at will within the shell program.

A major difference between shell programming and other programming languages is that in shell programming, variables are not typed — that is, you do not have to specify whether a variable is a number or a string, and so on.

Assigning a Value to a Variable

Assume that you want to use a variable called lcount to count the number of iterations in a loop within a shell program. You can declare and initialize this variable as follows:

Command Environment
lcount=0 bash
set lcount=0 tcsh
NOTE

Under bash, you must ensure that the equal sign (=) does not have spaces before and after it.

To store a string in a variable, you can use the following:

Command Environment
myname=Andrew bash
set myname=Andrew tcsh

Use the preceding variable form if the string doesn't have embedded spaces. If a string has embedded spaces, you can do the assignment as follows:

Command Environment
myname='Andrew Hudson' bash
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату