Bash — это название оболочки Unix, которая также распространялась как оболочка для операционной системы GNU и как оболочка по умолчанию в большинстве искажений Linux. Почти все приведенные ниже примеры могут быть частью сценария оболочки или выполняться непосредственно в оболочке.
first line of the script is the shebang which tells the system how to execute: !/usr/bin/env bash simple example hello world: echo hello world #Declaring a variable look like this: Variable= “some string” echo $variable → some string echo “$variable” → some variable echo ‘$variable’ → $variable Parameter expansion: echo ${Variable} → some string. this is a simple use of parameter expansion. parameter expansion gets a value from a variable. it “expands” or prints the value. During the expansion time the value or parameter can be modified. string substitution in variables: echo ${variable/some/A} → this will substitute the first occurence of “some” with “A”. length=7 → echo ${variable:0:length} this will return only the first 7 characters of the value. array0=(one two three five) → declare an array with 5 elements. echo $array0 echo ${array0[0]} || for i in “${array0[@]}”; do echo “$i” done|| → print all elements . each of them on new line. echo “ i’m in $(pwd)” → execs pwd and interpolates output. echo “i’m in $PWD” → interpolates the variable || if [ $name! =$USER] then echo “your name isn’t your username” else echo “your name is your username” || → we have the usual IF structure. use ‘man test’ for more info about conditional. true if the value of $name is not equal to the current user’s login username. alias ping = ‘ping -c 5’ → redefine command ‘ping’ as alias to send only 5 packets. \ping 192.168.1.1 → Escape the alias and use command with this name instead # You can also define functions # Definition: function foo () { echo "Arguments work just like script arguments: $@" echo "And: $1 $2..." echo "This is a function" returnValue=0 # Variable values can be returned return $returnValue } # Call the function `foo` with two arguments, arg1 and arg2: foo arg1 arg2 # => Arguments work just like script arguments: arg1 arg2 # => And: arg1 arg2... # => This is a function # Return values can be obtained with $? resultValue=$? # More than 9 arguments are also possible by using braces, e.g. ${10}, ${11}, ... # or simply bar () { echo "Another way to declare functions!" return 0 } # Call the function `bar` with no arguments: bar # => Another way to declare functions! # Calling your function foo "My name is" $Name # There are a lot of useful commands you should learn: # prints last 10 lines of file.txt tail -n 10 file.txt # prints first 10 lines of file.txt head -n 10 file.txt # sort file.txt's lines sort file.txt # report or omit repeated lines, with -d it reports them uniq -d file.txt # prints only the first column before the ',' character cut -d ',' -f 1 file.txt # replaces every occurrence of 'okay' with 'great' in file.txt # (regex compatible) sed -i 's/okay/great/g' file.txt # be aware that this -i flag means that file.txt will be changed # -i or --in-place erase the input file (use --in-place=.backup to keep a back-up) # print to stdout all lines of file.txt which match some regex # The example prints lines which begin with "foo" and end in "bar" grep "^foo.*bar$" file.txt # pass the option "-c" to instead print the number of lines matching the regex grep -c "^foo.*bar$" file.txt # Other useful options are: grep -r "^foo.*bar$" someDir/ # recursively `grep` grep -n "^foo.*bar$" file.txt # give line numbers grep -rI "^foo.*bar$" someDir/ # recursively `grep`, but ignore binary files # perform the same initial search, but filter out the lines containing "baz" grep "^foo.*bar$" file.txt | grep -v "baz" # if you literally want to search for the string, # and not the regex, use `fgrep` (or `grep -F`) fgrep "foobar" file.txt # The `trap` command allows you to execute a command whenever your script # receives a signal. Here, `trap` will execute `rm` if it receives any of the # three listed signals. trap "rm $TEMP_FILE; exit" SIGHUP SIGINT SIGTERM # `sudo` is used to perform commands as the superuser # usually it will ask interactively the password of superuser NAME1=$(whoami) NAME2=$(sudo whoami) echo "Was $NAME1, then became more powerful $NAME2" # Read Bash shell built-ins documentation with the bash `help` built-in: help help help help for help return help source help . # Read Bash manpage documentation with `man` apropos bash man 1 bash man bash # Read info documentation with `info` (`?` for help) apropos info | grep '^info.*(' man info info info info 5 info # Read bash info documentation: info bash info bash 'Bash Features' info bash 6 info --apropos bash
СПАСИБО…. !!