In Linux_use gnome-terminal_command_execute specified script_execute specified command_one-click recovery to terminal development environment

Article directory

  • 1. Use the shell script to open the terminal to the specified path to execute the specified command:
  • 2. Example of use:

1. Use the shell script to open the terminal to the specified path to execute the specified command:

1. Requirements:
    After learning how to use the gnome-terminal command, you can compile a shell script yourself to directly configure the terminal window.
    For example, every time I start up, I want to open ten working directories and run commands (such as opening working software, etc.), readers can write a shell script like this:

2. Usage:
  gnome-terminal [options...] [--commands...]
  
  Output gnome-terminal in the terminal, and a new terminal will pop up automatically. After the command is executed or an error is encountered, the new terminal will also flash back.
  
  Add exec bash in bash. In this way, the new terminal will not exit after executing the command.
  gnome-terminal --bash -c 'python3;exec bash'

    
3. Parameter description:
    Help options:
      -h, --help show help options
      --help-all show all help options
      --help-gtk show GTK+ options
      --help-terminal show terminal options
      --help-window-options show options for each window
      --help-terminal-options show options for each terminal

    GTK+ options
      --class=class The program class used by the window manager
      --name=name The name of the program used by the window manager
      --gdk-debug=flags GDK debug flags to set
      --gdk-no-debug=flags GDK debug flags to suppress
      --gtk-module=module load additional GTK+ modules
      --g-fatal-warnings turn warnings into fatal
      --gtk-debug=flags GTK+ debug flags to set
      --gtk-no-debug=flags GTK+ debug flags to suppress

    Options to open new windows or terminal tabs; more than one of these may be specified:
      --window open a new window containing the default configured next tab
      --tab open a new tab with the default configuration in the last opened window

    Window options; if used before the first --window or --tab argument, sets the default for all windows:
      --show-menubar open the menu bar
      --hide-menubar close the menu bar
      --maximize maximize window
      --full-screen full screen window
      --geometry=geometry set the window size; for example: 80x24, or 80x24 + 200 + 200 (columns x rows + X coordinates + Y coordinates)
      --role=role set window role
      --active Set the last specified tab page as the current tab page of the window

    Terminal options; if used before the first --window or --tab argument, sets the default for all terminals:
      -e, --command Execute the argument of this option in the terminal
      --profile=profilename use the provided profile instead of the default
      -t, --title=title set initial terminal title
      --working-directory=directory name set working directory
      --wait wait for child process to exit
      --fd=file descriptor forward file descriptor
      --zoom=zoom set the terminal's zoom factor (1.0 = normal size)

    Application options:
      --load-config=FILE load a terminal configuration file
      --preferences show preferences window
      -p, --print-environment print environment variables for interacting with the terminal
      -v, --verbose increase diagnostic verbosity
      -q, --quiet suppress output
      --display=display X display to use
            
            
            
            
    -t, --title=Title Set initial title for open terminal, easy to distinguish.
    -c,--command: -c refers to cmd, followed by the command to be executed. Multiple commands can be placed in double quotes and divided as the end symbol. If multiple commands are executed, they can be separated by semicolons, such as :"test1.sh; test2.sh"
     & amp; If you need to start multiple scripts in parallel at the same time, you can add the & amp; symbol after the command line to make the command run in the background.
    -e excute: It can appear multiple times. Note that there can only be one parameter after -e, that is to say, everything after -e needs to be enclosed in quotation marks. Such as: -e 'bash -c "pwd; exec bash;"'
                        Such as: gnome-terminal -e 'bash -c "ls; exec bash"'
                        
    exec bash: It is to let the opened terminal not close after the script is executed. If you write bash directly, it is equivalent to opening a subshell.
    --window: means to open a terminal window.
        Such as: gnome-terminal --window --window #Open two terminals
        Such as: gnome-terminal --window --tab --window --tab --tab #Open two terminals, the first two tabs, the second three tabs
    --tab: Create a new tab page in it, if you want more tab pages, continue to add --tab.
    --profile=profilename use the provided profile instead of the default
    --working-directory=directory name set working directory
    --wait wait for child process to exit
    --fd=file descriptor forward file descriptor
    --zoom=zoom set the terminal's zoom factor (1.0 = normal size)
    $

$
-x: The following is the type of command script to be executed in the opened terminal, you can modify it according to your needs, and -x can only appear once. The modifiable command types are as follows:
Such as: gnome-terminal -x bash -c “ls; exec bash”
1. SHELL: Shell, connecting users and Linux kernel, command line interpreter
2. BASH: A kind of shell, which belongs to the concept of linux system. The “black window” under linux system is called Bash shell
3. DOS: disk operating system, Windows simulates the DOS system, and the dos command is the underlying interactive command under the system
4. BAT file: BAT is the abbreviation of batch, bat file is a kind of batch processing file, also called BAT script, which includes batch commands (essentially dos command, cmd command); the suffix name “.ba or “. cmd”, which can be run through cmd.exe
5. CMD: The shell of Windows is cmd.exe, and the shell of Linux system is bash.exe
6. SH file: SH is the abbreviation of BASH. The sh file is also a batch file, also called a bash script, which includes bash commands (also called linux shell commands, shell commands); the suffix is “.sh” .
7. Roscore, roslaunch, etc.

 because -x and -e are deprecated.
        So it is replaced by the -- symbol,
        There is also the command bach -c, because bash -c is executed in the newly started terminal, that is, the subshell is started.
        After the change, such as: gnome-terminal --tab -- bach -c "ls" & amp;"
                gnome-terminal --tab -- bash -c " pwd; exec bash;"


4. [Automatically execute commands after the terminal starts]
    There are two parameters to achieve this function, -e and -x, the difference between these two is:
        -e can appear multiple times.
            If it is in front of all --windows, it means it works on all windows and tabs.
            If it is behind --window or --tab, it means only execute for this tab;

            Note: There can only be one parameter after -e, that is to say, if there is a space, quotation marks are required


        -x can only appear once, and everything after -x is considered to be the command to be executed, so spaces can appear. These commands are executed for all tabs.

    Note: The \ continuation symbol cannot be followed by a comment, otherwise the content behind the comment cannot be executed.

2. Example of use:

#Use the script to restore to the development terminal environment used by one key
Example 1:
    Every time I start up, I want to open three working directories and run commands (such as opening working software, etc.), readers can write a shell script like this:
    gnome-terminal --window --working-directory=${dir1} --title='dir1' --command="bash -c pwd;bash" \
    --tab \
    --working-directory=${dir2} --title='dir2' --command="bash -c ls;bash" \
    --tab \
    #Open vscode
    --working-directory=/home --title='run_eclipse' --command="bash -c code;bash"

Example 2:
    Open a new terminal and execute the command
    gnome-terminal -t "title-name" -x bash -c "cd ~; source ~/.bashrc; ./my_script;exec bash;"