Bash - Script

Bash Liste Des Attaques Ovh

About

This page is about Os Shell scripts (with a accent on the Bash shell)

How to create a shell script

A Bash or Shell Script is a text file that:

  • has a shebang
  • has the executable permission.

File Extension

File extensions are meaningless in UNIX, unlike DOS, where EXE, COM, and BAT indicate executable files.

Linux uses a shebang to define the type of language.

Example of minimal bash text file

#!/bin/bash

Executable

The .sh extension denotes shell script files but doesn't make the script executable. See file permission

Management

Start a script

In another process

Unlike DOS, UNIX does not automatically look in the current directory for a file to execute.

You have to specify:

  • the full file name
/usr/local/scripts/myscript.sh
cd /usr/local/scripts/
$  ./myscript.sh

Unix/Linux search for executables only in directories identified in the PATH variable.

In the same process

If you want to get the variable back, you need to run the script in the same process. For this, you use:

Run and terminate (exec)

See Bash - Exec (No New Process) - builtin command

Run a script in the background

  • nohup is a POSIX command to ignore the HUP (hangup) signal, enabling the command to keep running after the user who issues the command has logged out.
  • By adding the ampersand (&) to the end of the command line, you start the application in background.

Run a script hosted on the internet

bash <(curl -f -L -sS https://example.com/myscript.sh) args

Example:

bash <(curl -f -L -sS https://raw.githubusercontent.com/pagespeed/ngx_pagespeed/master/scripts/build_ngx_pagespeed.sh) --help

Exit Value

Bash’s exit status is the exit status of the last command executed in the file script. If no commands are executed, the exit status is 0.

Timing a script

$SECONDS: The number of seconds the script has been running.

#!/bin/bash

TIME_LIMIT=10
INTERVAL=1

echo
echo "Hit Control-C to exit before $TIME_LIMIT seconds."
echo

while [ "$SECONDS" -le "$TIME_LIMIT" ]
do   #   $SECONDS is an internal shell variable.
  if [ "$SECONDS" -eq 1 ]
  then
    units=second
  else  
    units=seconds
  fi

  echo "This script has been running $SECONDS $units."
  #  On a slow or overburdened machine, the script may skip a count
  #+ every once in a while.
  sleep $INTERVAL
done

echo -e "\a"  # Beep!

exit 0

Arguments

Bash - (Argument|Positional Parameter)

Get the script Name

The special parameter 0 Expands to the name of the shell or shell script. This is set at shell initialization. If bash is invoked with a file of commands, $0 is set to the name of that file.

If bash is started with the -c option, then $0 is set to the first argument after the string to be executed, if one is present. Otherwise, it is set to the file name used to invoke bash, as given by argument zero.

# echo $0
/bin/bash

Get the script directory

  • for a file
echo $(dirname $0)
# saw also
echo $( cd $(dirname $0) ; pwd -P )
echo $( dirname $(realpath "$0") )

Absolute path name

See the underscore special parameter $_

# $() run a subshell therefore there the cd command has no effect
SCRIPT_PATH=$( cd $(dirname $0) ; pwd -P )

Checking syntax

With the -n or -o noexec option of the set command, the command are not executed and you can then check a shell script for syntax errors.

Configuration

$BASH_ENV

An environmental variable pointing to a Bash startup file to be read when a script is invoked

1) 2)





Discover More
Bash Liste Des Attaques Ovh
Bash - Alias (of a command) - Builtin command

Alias allows to define shortcuts and synonyms for commonly used: shell commands (of group of command) or script The basic syntax is: where: p is to only print the aliases [name[=value]...
Bash Liste Des Attaques Ovh
Bash - Bash cli

Bash is an sh-compatible a shell that executes commands read: from the standard input or from a file (script). In addition to the single-character shell options documented in the description...
Bash Liste Des Attaques Ovh
Bash - Caller - Stack Trace (Builtin command)

Caller is a builtin command that returns the context (localization) of any active subroutine call (a shell function or a script executed with the . or source builtins. The current frame is frame...
Bash Liste Des Attaques Ovh
Bash - Command Execution

To execute a command in a script backticks in the same shell $( ) in a subshell from a string in a subshell from standard input See also: You can execute an expression inside an input...
Bash Liste Des Attaques Ovh
Bash - Disk Command

A disk command is a command that is not a builtin command and that can be found in the path. such as: a shell script a executable binary
Bash Liste Des Attaques Ovh
Bash - Script Execution

script execution
Bash Liste Des Attaques Ovh
Bash - Source - Built-in command

The source command and the point '.' are similar: Read and execute commands from filename in the current shell environment and return the exit status of the last command executed from filename. The...
Bash Liste Des Attaques Ovh
Bash - Special Parameter

Special parameters are Parameters treated specially. metacharacter ?? These parameters may only be referenced; assignment to them is not allowed. Positional parameter expansion Star () At...
Bash Liste Des Attaques Ovh
Bash - Subroutine (Stack)

A subroutine (in the call stack) is a shell function or script executed with . or source
Windows Envrionment Variable Path Shell Script
How to configure Git to use NotePad++ as editor on Windows?

This article shows you how you can configure git to use Notepad++ as editor on your laptop.



Share this page:
Follow us:
Task Runner