Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Shell script
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Capabilities== ===Comments=== [[Comment (computer programming)|Comments]] are ignored by the shell. They typically begin with the hash symbol (<code>#</code>), and continue until the end of the line.<ref name="Pro_Bash_Programming">{{cite book|last=Johnson|first=Chris|date=2009|url=https://books.google.com/books?id=NJmhi6T0nGMC&q=comment|title=Pro Bash Programming: Scripting the Linux Shell|publisher=Apress|access-date=September 27, 2019|isbn=9781430219989}}</ref> ===Configurable choice of scripting language=== The [[Shebang (Unix)|shebang]], or hash-bang, is a special kind of comment which the system uses to determine what interpreter to use to execute the file. The shebang must be the first line of the file, and start with "<code>#!</code>".<ref name=Pro_Bash_Programming /> In Unix-like operating systems, the characters following the "<code>#!</code>" prefix are interpreted as a path to an executable program that will interpret the script.<ref>{{Cite web|url=https://linux.die.net/man/3/execve|title=exec(3p) – POSIX Programmer's Manual|access-date=2020-07-24}}</ref> ===Shortcuts=== A shell script can provide a convenient variation of a system command where special environment settings, command options, or post-processing apply automatically, but in a way that allows the new script to still act as a fully normal [[Unix command]]. One example would be to create a version of [[ls]], the command to list files, giving it a shorter command name of <code>l</code>, which would be normally saved in a user's <code>bin</code> directory as <code>/home/''username''/bin/l</code>, and a default set of command options pre-supplied. <syntaxhighlight lang="sh"> #!/bin/sh LC_COLLATE=C ls -FCas "$@" </syntaxhighlight> Here, the first line uses a [[#Configurable choice of scripting language|shebang]] to indicate which interpreter should execute the rest of the script, and the second line makes a listing with options for file format indicators, columns, all files (none omitted), and a size in blocks. The <code>LC_COLLATE=C</code> sets the default collation order to not fold upper and lower case together, not intermix [[dotfile]]s with normal filenames as a side effect of ignoring punctuation in the names (dotfiles are usually only shown if an option like <code>-a</code> is used), and the <code>"$@"</code> causes any parameters given to <code>l</code> to pass through as parameters to ls, so that all of the normal options and other [[programming language syntax|syntax]] known to ls can still be used. The user could then simply use <code>l</code> for the most commonly used short listing. Another example of a shell script that could be used as a shortcut would be to print a list of all the files and directories within a given directory. <syntaxhighlight lang="sh"> #!/bin/sh clear ls -al </syntaxhighlight> In this case, the shell script would start with its normal starting line of <span style="font-family:courier">#!/bin/sh</span>. Following this, the script executes the command <span style="font-family:courier">clear</span> which clears the terminal of all text before going to the next line. The following line provides the main function of the script. The <span style="font-family:courier">ls -al</span> command lists the files and directories that are in the directory from which the script is being run. The <span style="font-family:courier">[[ls]]</span> command attributes could be changed to reflect the needs of the user. ===Batch jobs=== Shell scripts allow several commands that would be entered manually at a command-line interface to be executed automatically, and without having to wait for a user to trigger each stage of the sequence. For example, in a directory with three C source code files, rather than manually running the four commands required to build the final program from them, one could instead create a script for [[POSIX]]-compliant shells, here named <code>build</code> and kept in the directory with them, which would compile them automatically: <syntaxhighlight lang="sh"> #!/bin/sh printf 'compiling...\n' cc -c foo.c cc -c bar.c cc -c qux.c cc -o myprog foo.o bar.o qux.o printf 'done.\n' </syntaxhighlight> The script would allow a user to save the file being edited, pause the editor, and then just run <code>./build</code> to create the updated program, test it, and then return to the editor. Since the 1980s or so, however, scripts of this type have been replaced with utilities like [[make (software)|make]] which are specialized for building programs. ===Generalization=== Simple batch jobs are not unusual for isolated tasks, but using shell loops, tests, and variables provides much more flexibility to users. A POSIX sh script to convert JPEG images to PNG images, where the image names are provided on the command-line—possibly via wildcards—instead of each being listed within the script, can be created with this file, typically saved in a file like <code>/home/''username''/bin/jpg2png</code> <syntaxhighlight lang="sh"> #!/bin/sh for jpg; do # use $jpg in place of each filename given, in turn png=${jpg%.jpg}.png # construct the PNG version of the filename by replacing .jpg with .png printf 'converting "%s" ...\n' "$jpg" # output status info to the user running the script if convert "$jpg" jpg.to.png; then # use convert (provided by ImageMagick) to create the PNG in a temp file mv jpg.to.png "$png" # if it worked, rename the temporary PNG image to the correct name else # ...otherwise complain and exit from the script printf >&2 'jpg2png: error: failed output saved in "jpg.to.png".\n' exit 1 fi # the end of the "if" test construct done # the end of the "for" loop printf 'all conversions successful\n' # tell the user the good news </syntaxhighlight> The <code>jpg2png</code> command can then be run on an entire directory full of JPEG images with just <code>/home/''username''/bin/jpg2png *.jpg</code> ===Programming=== Many modern shells also supply various features usually found only in more sophisticated [[general-purpose programming language]]s, such as control-flow constructs, variables, [[Comment (computer programming)|comments]], arrays, [[subroutine]]s and so on. With these sorts of features available, it is possible to write reasonably sophisticated applications as shell scripts. However, they are still limited by the fact that most shell languages have little or no support for data typing systems, classes, threading, complex math, and other common full language features, and are also generally much slower than compiled code or interpreted languages written with speed as a performance goal. The standard Unix tools [[sed]] and [[awk]] provide extra capabilities for shell programming; [[Perl]] can also be embedded in shell scripts as can other scripting languages like [[Tcl]]. Perl and Tcl come with graphics toolkits as well.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)