DESCRIPTION
Script makes a typescript of everything printed on your terminal. It is useful for students who need a hardcopy record of an interactive session as proof of an assignment, as the typescript file can be printed out later with lpr(1).
If the argument file is given, script saves all dialogue in file. If no file name is given, the typescript is saved in the file typescript.
Options:
Tag | Description |
---|---|
—a | Append the output to file or typescript, retaining the prior contents. |
—c COMMAND | |
Run the COMMAND rather than an interactive shell. This makes it easy for a script to capture the output of a program that behaves differently when its stdout is not a tty. | |
—f | Flush output after each write. This is nice for telecooperation: One person does mkfifo foo; script -f foo and another can supervise real-time what is being done using cat foo. |
—q | Be quiet. |
—t | Output timing data to standard error. This data contains two fields, separated by a space. The first field indicates how much time elapsed since the previous output. The second field indicates how many characters were output this time. This information can be used to replay typescripts with realistic typing and output delays. |
The script ends when the forked shell exits (a control-D to exit the Bourne shell ( sh(1)), and exit, logout or control-d (if ignoreeof is not set) for the C-shell, csh(1)).
Certain interactive commands, such as vi(1), create garbage in the typescript file. Script works best with commands that do not manipulate the screen, the results are meant to emulate a hardcopy terminal.
ENVIRONMENT
The following environment variable is utilized by script:
Tag | Description |
---|---|
SHELL | If the variable SHELL exists, the shell forked by script will be that shell. If SHELL is not set, the Bourne shell is assumed. (Most shells set this variable automatically). |
Linux script command
On Unix-like operating systems, the script command makes a typescript of the terminal session.
This page covers the Linux version of script.
Description
script makes a typescript of everything printed on your terminal. It is useful for users who need a hardcopy record of an interactive session as proof of work done, as the typescript file can be printed out later with lpr. If the file argument is given, script saves all dialogue in file. If no file name is given, the typescript is saved in a file named typescript.
Syntax
script [-a] [-c command] [-e] [-f] [-q] [-t[=file]] [-V] [-h] [file]
Options
-a, —append | Append the output to file or typescript, retaining the prior contents. |
-c, —command command | Run the command rather than an interactive shell. This option makes it easy for script to capture the output of a program that behaves differently when its standard output is not a tty. |
-e, —return | Return the exit code of the child process. Uses the same format as bash termination on signal termination: exit code is 128+n. |
-f, —flush | Flush output after each write. This option is excellent for telecooperation: one person does «mkfifo foo; script -f foo«, and another can supervise real-time what is being done using «cat foo«. |
—force | Allow the default output destination, i.e., the typescript file, to be a hard or symbolic link. The command follows a symbolic link. |
-q, —quiet | Operate without displaying any output. |
-t, —timing[=file] | Output timing data to standard error, or to file when given. This data contains two fields, separated by a space. The first field indicates how much time elapsed since the previous output. The second field indicates how many characters were output this time. This information can replay typescripts with realistic typing and output delays. |
-V, —version | Display version information, and exit. |
-h, —help | Display a help message, and exit. |
The script ends when the forked shell exits (a control-D to exit the Bourne shell (sh), and exit, logout or control-d (if ignoreeof is not set) for the C-shell, csh).
Certain interactive commands, such as vi, create garbage in the typescript file. script works best with commands that do not manipulate the screen; the results are meant to emulate a hardcopy terminal.
Examples
Logs all results to file myfile.txt. This command opens a subshell and records all information through this session. The script ends when the forked shell exits (e.g., when the user types exit) or when Ctrl-D is typed.
The output will be written to the file ./myfile.txt, and the last line of the file will log the date and time the command was executed.
script -c "ps ax" /tmp/processes.txt
Logs the output of the ps command, using the a and x options, which will force it to list all current processes on the system you can see. The output will be written to the file /tmp/processes.txt, and the last line of the file will log the date and time the command was executed.
script -c "echo \"Hello, World!\"" hello.txt
Logs the output of the echo command that echoes the text «Hello, World!«. Since the entire command must appear in quotes, and the command contains quotes itself, the quotes in the command are escaped with a backslash. This «protects» them from the shell, telling it to treat them as literal quotes. Alternatively, we could have used single-quotes to enclose the command, and double-quotes inside the command to differentiate them.
The output will be written to the file ./hello.txt, and the last line of the file will log the date and time the command was executed.
script -c 'echo "Hello, World!"' hello.txt
Same as the example above.
How To Execute a Command with a Shell Script in Linux
Shell is a command-line interpreter that allows the user to interact with the system. It is responsible for taking inputs from the user and displaying the output.
Shell scripts are a series of commands written in order of execution. These scripts can contain functions, loops, commands, and variables. Scripts are useful for simplifying a complex series of commands and repetitive tasks.
In this article, you will learn how to create and execute shell scripts for the command line in Linux.
Prerequisites
To complete this tutorial, you will need:
- Familiarity with using the terminal.
- Familiarity with a text editor.
- Familiarity with commands like chmod , mkdir , and cd .
Getting Started
A shell script needs to be saved with the extension .sh .
The file needs to begin with the shebang line ( #! ) to let the Linux system know which interpreter to use for the shell script.
For environments that support bash , use:
For environments that support shell , use:
This tutorial assumes that your environment supports bash .
Shell scripts can also have comments to increase readability. A good script always contains comments that help a reader understand exactly what the script is doing and the reasoning behind a design choice.
Creating and Running a Basic Shell Script
You can create a shell script using the vi editor, a cat command, or a text editor.
For this tutorial, you will learn about creating a shell script with vi :
This starts the vi editor and creates a basic_script.sh file.
Then, press i on the keyboard to start INSERT MODE . Add the following lines:
This script runs the commands whoami and date . whoami displays the active username. date displays the current system timestamp.
To save and exit the vi editor:
Finally, you can run the script with the following command:
You may get output that resembles the following:
Outputroot Fri Jun 19 16:59:48 UTC 2020
The first line of output corresponds to the whoami command. The second line of output corresponds to the date command.
You can also run a script without specifying bash :
Running the file this way might require the user to give permission first. Running it with bash doesn’t require this permission.
Output~bash: ./basic_script.sh: Permission denied
The command bash filename only requires the read permission from the file.
Whereas the command ./ filename , runs the file as an executable and requires the execute permission.
To execute the script, you will need to update the permissions.
This command applies chmod and gives x (executable) permissions to the current user.
Using Variables in Shell Scripts
Scripts can include user-defined variables. In fact, as scripts get larger in size, it is essential to have variables that are clearly defined and that have self-descriptive names.
Add the following lines to the script:
#!/bin/bash # This is a comment # defining a variable GREETINGS="Hello! How are you" echo $GREETINGS
GREETINGS is the variable defined and later accessed using $ (dollar sign symbol. There should be no space in the line where variables are being assigned a value.
This prints out the value assigned to the variable:
When the script is run, GREETINGS is defined and accessed.
Reading Input from the Command Line
Shell scripts can be made interactive with the ability to accept input from the command line. You can use the read command to store the command line input in a variable.
Add the following lines to the script:
#!/bin/bash # This is a comment # defining a variable echo "What is your name?" # reading input read NAME # defining a variable GREETINGS="Hello! How are you" echo $NAME $GREETINGS
A variable NAME has been used to accept input from the command line. This script waits for the user to provide input for NAME . Then it prints NAME and GREETINGS .
OutputWhat is your name? Sammy Sammy Hello! How are you
In this example, the user has provided the prompt with the name: Sammy .
Defining Functions
Users can define their own functions in a script. These functions can take multiple arguments.
Add the following lines to the script:
#!/bin/bash #This is a comment # defining a variable echo "What is the name of the directory you want to create?" # reading input read NAME echo "Creating $NAME . " mkcd () mkdir "$NAME" cd "$NAME" > mkcd echo "You are now in $NAME"
This script asks the user for a directory name. Then, it uses mkdir to create the directory and cd into it.
OutputWhat is the name of the directory you want to create? test_dir Creating test_dir . You are now in test_dir
In this example, the user has provided the prompt with the input: test_dir . Next, the script creates a new directory with that name. Finally, the script changes the user’s current working directory to test_dir .
Conclusion
In this article, you learned how to create and execute shell scripts for the command line in Linux.
Consider some repetitive or time-consuming tasks that you frequently perform that could benefit from a script.
Continue your learning with if-else , arrays, and arguments in the command line.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.