Linux Commands Examples

A great documentation place for Linux commands

script

make typescript of terminal session


see also : scriptreplay

Synopsis

script [-a] [-c command] [-e] [-f] [-q] [-t[=file]] [-V] [-h] [file]


add an example, a script, a trick and tips

: email address (won't be displayed)
: name

Step 2

Thanks for this example ! - It will be moderated and published shortly.

Feel free to post other examples
Oops ! There is a tiny cockup. A damn 404 cockup. Please contact the loosy team who maintains and develops this wonderful site by clicking in the mighty feedback button on the side of the page. Say what happened. Thanks!

examples

0
source

How do perform commands in another folder, without repeating the folder path?

Run the operation in a subshell.

( cd /folder1/folder2/folder3 && mv file.txt file-2013.txt )

The change of working directory won't be propagated to the parent shell.

0
source

How to execute a command whenever a file changes?

A little more on the programming side, but you want something like inotify. There are implementations in many languages, such as jnotify and pyinotify.

This library allows you to monitor single files or entire directories, and returns events when an action is discovered. The information returned includes the file name, the action (create, modify, rename, delete) and the file path, among other useful information.

0
source

What is the difference between executing a bash script and sourcing a bash script?

Sourcing you get all the extra variables defined in the script.
So if you have configs or function definitions you should source and not execute. Executions are independent from the parents environment.

0
source

Looping Through Subdirectories and Running a Command in Each

for dir in ~/projects/git/*; do (cd "$dir" && git pull); done

0
source

Create a logoff script/task for Linux

The C-shell, a.k.a. csh, has the .logout functionality, and Bash (the more current default) has .bash_logout, but I don't remember any other shells (ksh, sh, etc) with a similar concept.

0
source

How to set a Linux Ditribution to self-destruct (to wipe everything from the system partition) via a script

To destroy everything seems to be a little overkill, how about to just remove/unstall your program.

rm -rf /path/to/your/program/

Or how about you implement a normal licensing server that the program must contact before it can start?


Update: As a open question, do you plan to destroy the users data as well as your program? Or are the users data stored elsewhere?

And maybe the user should get some kind of nagware notice that you plan destroy everything! Something like

-"This software will self destruct if you don't pay more money, you have X days left."

As a user I would be really upset if you just destroyed something in my computer without even telling me that this was about to happen (so I at least got some chance of affecting the outcome).

It is kind of bad for your company if all your paying costumers would end up hating you.

0
source

Multiple standard input? How?

<(...) does process substitution in bash. The output of the process in the parens is sent to an additional file descriptor beyond the normal 3, and a filename is returned corresponding to that file descriptor. In this way the output of a command can be treated as a filename to be passed to another command.

0
source

Can I make a script always execute as root?

Be really careful: scripts combined with setuid are dangerous!

First, please have a look on this question/answers, especially on this answer and security warning.

If you still want to execute your script with setuid set, then you can write a short C program as wrapper and set the setuid bit on the compiled binary.

Wrapper example:

int main(void) {        
    setuid(0);
    clearenv();
    system("/absolute/path/to/your/script.sh");
}


Another solution using sudo (mentioned here):

  1. As root, prevent write (and maybe other) access to your script:

    chown root /absolute/path/to/your/script.sh
    chmod 700 /absolute/path/to/your/script.sh
    
  2. Verify that noone except root can replace the script, e.g. by modifying the access rights of the parent folder:

    chown root /absolute/path/to/your/
    chmod 755 /absolute/path/to/your/
    
  3. Modify sudo access rights in /etc/sudoers with visudo:

    ALL    ALL = (root) NOPASSWD: /absolute/path/to/your/script.sh
    

    More details about the settings (e.g. restricting access to specific users or groups) can be found in the sudoers manpage.

Afterwards, all users can run the script as root without password:

sudo /absolute/path/to/your/script.sh

This is similar to using the wrapper/setuid solution above.

0
source

How to extract text from pdf in script on Linux?

pdftotext that comes with poppler will try to extract any text found in the PDF.

0
source

Where should I put system-wide scripts in *nix system?

Well, your tags include the Filesystem Hierarchy Standard so that should give you some guidance. I would recommend /usr/local/bin.

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:

-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 makes it easy for a script to capture the output of a program that behaves differently when its stdout 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 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’.

--force

Allow the default output destination, i.e. the typescript file, to be a hard or symbolic link. The command will follow a symbolic link.

-q, --quiet

Be quiet.

-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 be used to replay typescripts with realistic typing and output delays.

-V, --version

Output version information and exit.

-h, --help

Output help and exit.

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.

availability

The script command is part of the util-linux package and is available from ftp://ftp.kernel.org/pub/linux/utils/util-linux/.

util-linux February 2011 util-linux

environment

The following environment variable is utilized by script:

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).

c-

--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 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’.

h-

--help

Output help and exit.

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.

t-

--version

Output version information and exit.


bugs

Script places everything in the log file, including linefeeds and backspaces. This is not what the naive user expects.


history

The script command appeared in 3.0BSD.


see also

csh (for the history mechanism), scriptreplay .

How can this site be more helpful to YOU ?


give  feedback