Linux command exec file

The “exec” Command in Linux [8 Practical Examples]

The exec command in Linux is an easy-to-use command. One can use it for multiple purposes. The exec command replaces the current terminal process with a new command. This command in Linux often executes specific programs or commands without creating a new process. In this article, I will demonstrate to you the ins and outs of the exec command in Linux.

A. Description

The exec command can be used for multiple purposes in Linux. The main purpose of the command is to replace the current process with a new command that takes over the memory, process ID, and some other resources. If the exec command is executed without any other command, it terminates the terminal. So, the bash command needs to be executed before that.

B. Syntax

The exec command has a very simple syntax in Linux. It includes the exec command, options & filename. And the syntax is as follows:

Note: In the syntax above, OPTION is enclosed by a square bracket and followed by 3 dots representing that more than one option can be used at the same time. Besides,3 dots after ARGUMENT suggest that you can specify multiple arguments.

C. Options

A few options are available for the exec command. I have mentioned one of the options of the command here. However, you can explore the help page for the exec command to know more about its options.

Note: The options in Linux CLI (Command Line Interface) are case-sensitive, so be cautious when you use them.

Practical Examples of the “exec” Command in Linux

The exec command in Linux is a pretty straightforward command which replaces the current terminal process with a new command. Here you will learn some useful examples of the exec command in Linux.

Example 1: List the Contents of the Current Directory in Long Format Using the “exec” Command in Linux

Using the exec command you can list the contents of the current directory in Linux. To list the contents of my current directory, I am using the exec command in Linux. To do the same you can follow the procedure below:

Steps to Follow:

➊ Firstly launch the Ubuntu Terminal.

➋ Run the command below in the command prompt:

➌ Now, press the ENTER button.

❹Copy the following command in the command prompt:

➎ Then, tap the ENTER button.

List the Contents of the Current Directory in Long Format Using the “exec” Command in Linux

As you can see, the contents of the current directory are listed in the output.

Note: Before running the exec command you need to run the bash command otherwise, the terminal will exit.

Читайте также:  Get file hash linux

Similar Readings

Example 2: List All Environment Variables And Their Values

Using the exec command you can also list the environment variables and their values in Linux. I am using the exec command in Linux to list the environment variables and their values. To do the same you can follow the process below:

Steps to Follow:

➊ At first open the Terminal in Ubuntu.

➋ Write the following command in the command prompt:

➌ Now, hit the ENTER key.

❹ Type the command below in the command prompt:

➎ Then, press the ENTER button.

List All Environment Variables And Their Values

In the image below, the environment variables and their values are listed in the output.

Note: You need to execute the bash command before executing the exec command or else, the terminal will be terminated.

Example 3: Call Program in Bash Script Using the “exec” Command in Linux

In the bash script, a program can be called using the exec command in Linux. I am using the exec command inside a program in the script named script.sh in Linux. To do this you can follow the steps below:

Steps to Follow:

➊ Initially open the Ubuntu Terminal.

➋ Write the following command in the command prompt:

Call Program in Bash Script Using the “exec” Command in Linux

➌ Now, hit the ENTER key.

❹ Type the command below in the text editor:

#!/bin/bash while true do echo "1) Update " echo "2) Upgrade " echo "3) Exit" read Input case "$Input" in 1) exec sudo apt update ;; 2) exec sudo apt upgrade ;; 3) break esac done

Call Program in Bash Script Using the “exec” Command in Linux

➎ Then save the script and exit by pressing the CTRL + S and CTRL + X buttons.

❻ Copy the command below in the command prompt:

❼ After that, tap the ENTER button.

Call Program in Bash Script Using the “exec” Command in Linux

In the following image, the output shows that the program of the script named script.sh is called. You can type 1, 2, or 3 and press the ENTER key to run the update, upgrade, or exit function.

Example 4: Log Stdout & Stderr In the Bash Script Using the “exec” Command in Linux

You can log the standard output and the standard error in the bash script using the exec command in Linux. To log standard output & standard error “>>” and “>&” are used consecutively. I am using the exec command to log the standard output and the standard error in the script named logging.sh in Linux. You can also do this by following the instructions below:

Steps to Follow:

➊ To start, launch the Ubuntu Terminal.

➋ Run the following command in the command prompt:

Log Stdout & Stderr In the Bash Script Using the “exec” Command in Linux

➌ Then, hit the ENTER key.

❹ Type the following command in the text editor:

#!/bin/bash # Create testFile.log file touch testFile.log # Save testFile.log to log_file variable log_file="testFile.log" # Redirect stdin to $log_file exec >>$log_file # Redirect stderr to the same place as stdin exec 2>&1 echo "This line is the first line of the log file" echo "This line is the second line of the log file" eho "This line is the third line and has an error"

Log Stdout & Stderr In the Bash Script Using the “exec” Command in Linux

➎ Then save the script and exit by pressing the CTRL + S and CTRL + X buttons.

❻ To give execution permission to the logging.sh script, copy the following command in the command prompt:

Log Stdout & Stderr In the Bash Script Using the “exec” Command in Linux

❼ Now, strike the ENTER button.

❽ After that, write the following command in the command prompt:

Log Stdout & Stderr In the Bash Script Using the “exec” Command in Linux

❾ Then, tap the ENTER key.

❿ Run the command below and press the ENTER button.

Log Stdout & Stderr In the Bash Script Using the “exec” Command in Linux

In the image beneath, the output shows that the textFile.log file contains the standard output and the standard error.

Читайте также:  Linux mint with lxde

Example 5: Create a Clean Environment By Running Script

You can create a clean environment using the exec command with the printenv command and option -c in Linux. I am using the exec command in Linux to clean the environment variables and their values. You can also do the same by following the steps below:

Steps to Follow:

➊ Initially open the Terminal in Ubuntu.

➋ Write the following command in the command prompt and hit the ENTER key.

Create a Clean Environment By Running Script

This command prints all environment variables and their values in the terminal.

➌ Now, Type the command below in the command prompt:

❹ Then, press the ENTER button.

➎ After that, copy the following command in the command prompt:

❻ At last, tap the ENTER key.

In the following image, as you can see in the output the environment variables and their values are cleaned by using the exec command with the printenv command and option -c.

Note: You need to execute the bash command before executing the exec command if not, the terminal will exit.

Example 6: Search Files And Display Their Contents Using the “exec” Command in Linux

You can search the desired files using the find command & display the contents of the files using the cat command in Linux. And the exec command uses the output of the find command as the input of the cat command. Here I want to search all the .txt files of the MyFolder/ directory which is inside the home directory using the find command and use the output as the cat command’s input using the exec command. To do the same you can follow the procedure below:

Steps to Follow:

➊ At first, launch the Terminal in Ubuntu.

➋ Copy the following command in the command prompt:

Find ~/MyFolder -name “*.txt” -exec cat <> \;

➌ Then, strike the ENTER key.

Search Files And Display Their Contents Using the “exec” Command in Linux

As you can see in the image, the terminal is displaying the contents of all the .txt files searched by the find command.

Example 7: Search Files And Compress Those Using the “exec” Command in Linux

The expected files can be searched using the find command & compress each file that is found using the gzip command in Linux. And the exec command uses the output of the find command as the input of the gzip command. Here I want to search all the .log files of the MyFolder/ directory which is inside the home directory using the find command and using the exec command to forward the output as the gzip command’s input. To do the same you can follow the method below:

Steps to Follow:

➊ At first, launch the Terminal in Ubuntu.

➋ Type the following command in the command prompt and press the ENTER button.

➌ Copy the following command in the command prompt:

Find ~/MyFolder -name “*.log” -exec gzip <> \;

❹ Then, strike the ENTER key.

❺ Write the following command in the command prompt:

❻ After that, hit the ENTER key.

Search Files And Compress Those Using the “exec” Command in Linux

As you can see in the image, all the .log files searched by the find command are compressed using the gzip command.

Example 8: Search Files And Change Permission

You can give permission to each file that is found using the chmod command in Linux. And the exec command forwards the output of the find command to the input of the chmod command. Here I want to change the permission of all the .log files of the MyFolder/ directory which is inside the home directory. You can also do the same by following the process below:

Читайте также:  Создаем образ своей линукс

Steps to Follow:

➊ At first, open the Terminal in Ubuntu.

➋ Write the following command in the command prompt and press the ENTER button.

➌ Run the following command in the command prompt:

Find ~/MyFolder -name “*.log” -exec chmod +x <> \;

❹ Then, strike the ENTER key.

❺ Copy the following command in the command prompt:

❻ After that, hit the ENTER key.

Search Files And Change Permission

As you can see in the image, the permission of all the .log files searched by the find command is changed by using the chmod command.

Conclusion

As seen in this article, the exec command has some significant uses in Linux. You’ve also found the syntax, some functional options, and the practical applications of this command. To become an expert in Linux, practice the command and its practical applications.

Similar Readings

  • The “awk” Command in Linux [11+ Practical Examples]
  • The “jobs” Command in Linux [6 Practical Examples]
  • The “make” Command in Linux [5 Practical Examples]
  • The “sleep” Command in Linux [10 Practical Examples]
  • The “xargs” Command in Linux [5 Practical Examples]

Источник

Linux command exec file

NAME

exec - execute commands and open, close, or copy file descriptors

SYNOPSIS

exec [command [argument . ]] 

DESCRIPTION

The exec utility shall open, close, and/or copy file descriptors as specified by any redirections as part of the command. If exec is specified without command or arguments, and any file descriptors with numbers greater than 2 are opened with associated redirection statements, it is unspecified whether those file descriptors remain open when the shell invokes another utility. Scripts concerned that child shells could misuse open file descriptors can always close them explicitly, as shown in one of the following examples. If exec is specified with command, it shall replace the shell with command without creating a new process. If arguments are specified, they shall be arguments to command. Redirection affects the current shell execution environment.

OPTIONS

OPERANDS

STDIN

INPUT FILES

ENVIRONMENT VARIABLES

ASYNCHRONOUS EVENTS

STDOUT

STDERR

The standard error shall be used only for diagnostic messages.

OUTPUT FILES

EXTENDED DESCRIPTION

EXIT STATUS

If command is specified, exec shall not return to the shell; rather, the exit status of the process shall be the exit status of the program implementing command, which overlaid the shell. If command is not found, the exit status shall be 127. If command is found, but it is not an executable utility, the exit status shall be 126. If a redirection error occurs (see Consequences of Shell Errors ), the shell shall exit with a value in the range 1-125. Otherwise, exec shall return a zero exit status.

CONSEQUENCES OF ERRORS

Default. The following sections are informative. 

APPLICATION USAGE

EXAMPLES

Open readfile as file descriptor 3 for reading: exec 3 readfile Open writefile as file descriptor 4 for writing: exec 4> writefile Make file descriptor 5 a copy of file descriptor 0: exec 5 Close file descriptor 3: exec 3 Cat the file maggie by replacing the current shell with the cat utility: exec cat maggie 

RATIONALE

Most historical implementations were not conformant in that: foo=bar exec cmd did not pass foo to cmd.

FUTURE DIRECTIONS

SEE ALSO

Special Built-In Utilities 

© 2019 Canonical Ltd. Ubuntu and Canonical are registered trademarks of Canonical Ltd.

Источник

Оцените статью
Adblock
detector