├── README.md ├── hello.sh ├── 016_Fot_Loop_with_Commands.sh ├── 014_Until_Loop.sh ├── 031_python.sh ├── 015_For_Loops.sh ├── 032_python2.sh ├── 033_exercises.sh ├── 006_Logical_OR_operator.sh ├── 005_logical_AND_operator.sh ├── 030-script_class.sh ├── 013_Read_file_content.sh ├── 017_Select_Loop.sh ├── 007_Aritmetic_Operation.sh ├── 002_Pass_argument_to_BashScri.sh ├── 011_Array_Variables.sh ├── 009_Case_Statement.sh ├── 008_Aritmetic_Operation_with_Floating_Numbers.sh ├── 010_Case_Statement _example.sh ├── 012_While_Loops.sh ├── 003_If_Statements.sh ├── 001_read_input_on_terminal.sh ├── 004_File_Test_Operators.sh ├── integer_comparision.md └── bash.sh /README.md: -------------------------------------------------------------------------------- 1 | # Shell_Scripting -------------------------------------------------------------------------------- /hello.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | #while loops reading a file 4 | 5 | while read p 6 | do 7 | echo $p 8 | done < hello.sh 9 | -------------------------------------------------------------------------------- /016_Fot_Loop_with_Commands.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | for item in * 4 | do 5 | if [ -f $item ] 6 | then 7 | echo $item 8 | fi 9 | done -------------------------------------------------------------------------------- /014_Until_Loop.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | #until loops 3 | 4 | n=1 5 | until [ $n -ge 10 ] # (( $n > 10 )) 6 | do 7 | echo $n 8 | (( ++n )) 9 | done 10 | -------------------------------------------------------------------------------- /031_python.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | num1=2426 3 | num2=2150 4 | 5 | code=$(cat< echo $1 $2 $3 " 4 | # the other way ************************************ 5 | args=("$@") #decorator parse all argument when the user enter variables when execute the program 6 | 7 | # alternate way to show the arguments ************** 8 | echo ${args[1]} ${args[2]} 9 | 10 | #echo $@ # print all argument that user enter when execute the script 11 | 12 | #echo $# #write the number of the arguments 13 | 14 | -------------------------------------------------------------------------------- /011_Array_Variables.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | os=('ubuntu' 'windows' 'ios' 'kali') 3 | os[0]="mac" #add or update element 4 | unset os[3] # remove the element 5 | 6 | echo "${os[0]}" 7 | echo "${os[1]}" 8 | echo "${os[2]}" 9 | echo "${os[@]}" # @ isareti ile hepsini yazdiriyoruz. 10 | echo "${!os[@]}" # !os[@] ile liestnin indexini yazdiriyoruz. 11 | echo "${#os[@]}" # ile listenin eleman sayisini yazdiriyoruz 12 | 13 | 14 | string=asfasfasdfasdfasdf 15 | echo "${string[@]}" 16 | echo "${string[0]}" 17 | echo "${string[1]}" 18 | 19 | -------------------------------------------------------------------------------- /009_Case_Statement.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | #case expression in 4 | # pattern1 ) 5 | # statements ;; 6 | # pattern2 ) 7 | # statements ;; 8 | # .... 9 | #esac 10 | 11 | vehicle=$1 12 | 13 | case $vehicle in 14 | "car" ) 15 | echo "Rent of $vehicle is 100 dollar" ;; 16 | "van" ) 17 | echo "Rent of $vehicle is 80 dollar" ;; 18 | "bicycle" ) 19 | echo "Rent of $vehicle is 5 dollar" ;; 20 | "truck" ) 21 | echo "Rent of $vehicle is 150 dollar" ;; 22 | * ) 23 | echo "Unknown vehicle" ;; 24 | esac -------------------------------------------------------------------------------- /008_Aritmetic_Operation_with_Floating_Numbers.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | num1=20.5 4 | num2=5 5 | 6 | # we have to use bc library on shell 7 | 8 | echo "20.5+5" | bc 9 | echo "20.5-5" | bc 10 | echo "20.5*5" | bc 11 | echo "scale=2; 20.5/5" | bc # kac basamka gormek istiyorsan scale ile tanimliyoruz. 12 | echo "20.5%5" | bc 13 | echo "$num1+$num2" | bc 14 | echo "$num1-$num2" | bc 15 | 16 | 17 | # square root 18 | echo "square root" 19 | 20 | num=27 21 | echo "scale=2; sqrt($num)" | bc -l 22 | 23 | #power of something 24 | echo "power" 25 | num=3 26 | echo "scale=2; 3^3" | bc -l 27 | -------------------------------------------------------------------------------- /010_Case_Statement _example.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | #case expression in 4 | # pattern1 ) 5 | # statements ;; 6 | # pattern2 ) 7 | # statements ;; 8 | # .... 9 | #esac 10 | 11 | 12 | echo -e "Enter some character: \c" 13 | read value 14 | 15 | case $value in 16 | [a-z] ) 17 | echo "User entered $value a to z" ;; 18 | [A-Z] ) 19 | echo "User entered $value A to Z" ;; 20 | [0-9] ) 21 | echo "User entered $value 0 to 9" ;; 22 | ? ) 23 | echo "User entered $value special character" ;; 24 | * ) 25 | echo "Unknown chaaracter" ;; 26 | esac -------------------------------------------------------------------------------- /012_While_Loops.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | #while loops 3 | 4 | n=1 5 | while [ $n -le 10 ] # (( $n <= 10 )) 6 | do 7 | echo "$n" 8 | #n=$(( n+1 )) 9 | (( n++ )) 10 | #(( ++n )) 11 | done 12 | 13 | 14 | #//*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/ 15 | 16 | n=1 17 | while [ $n -le 10 ] # (( $n <= 10 )) 18 | do 19 | echo "$n" 20 | #n=$(( n+1 )) 21 | (( n++ )) 22 | #(( ++n )) 23 | sleep 1 24 | done 25 | 26 | #/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/ 27 | 28 | n=1 29 | while [ $n -le 3 ] # (( $n <= 10 )) 30 | do 31 | echo "$n" 32 | #n=$(( n+1 )) 33 | (( n++ )) 34 | #(( ++n )) 35 | gnome-terminal & 36 | done 37 | -------------------------------------------------------------------------------- /003_If_Statements.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | # if statement 3 | 4 | #if [ conditional expression ] 5 | #then 6 | # statement 7 | #else 8 | # statement 9 | #fi 10 | 11 | # examples */*/*/*/*/*/*/*/*/*/*/*/ 12 | 13 | count=10 14 | 15 | if [ $count -ne 9 ] 16 | then 17 | echo "correct" 18 | else 19 | echo "wrong" 20 | fi 21 | 22 | #*/*/*/*/*/*/*/*/*/*/*/*/*////*/*/*/*/*/ 23 | 24 | count=10 25 | 26 | if (( $count > 9 )) 27 | then 28 | echo "correct" 29 | else 30 | echo "wrong" 31 | fi 32 | 33 | #/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/ 34 | 35 | word=abc 36 | 37 | if [ $word == "abcccc" ] 38 | then 39 | echo "matched" 40 | else 41 | echo "not matched" 42 | fi -------------------------------------------------------------------------------- /001_read_input_on_terminal.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | #***********single entry************************* 4 | echo "Enter name : " 5 | read name 6 | echo "Entered name : $name" 7 | 8 | #********multiple entry************************** 9 | 10 | echo "Enter names : " 11 | read name1 name2 name3 12 | echo "Names : $name1 , $name2 , $name3" 13 | 14 | #********single line read command**************** 15 | 16 | read -p "username : " user_var 17 | echo "username : $user_var" 18 | 19 | #***********unseen password with -s flag********** 20 | 21 | read -p "username : " user_var 22 | read -sp "password : " pass_var 23 | echo 24 | echo "username : $user_var" 25 | echo "username : $pass_var" 26 | 27 | #**************read mutlipe var with list********* 28 | 29 | echo "Enter names : " 30 | read -a Names 31 | echo "Names : ${Names[0]}, ${Names[1]}, ${Names[2]}" 32 | 33 | #************************************************* 34 | -------------------------------------------------------------------------------- /004_File_Test_Operators.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | echo -e "Enter the name of file : \c" 4 | read file_name 5 | 6 | if [ -e $file_name ] # -e flasg control the file whether exist or not 7 | # -f flag is file exist whetheer file ir regular file or not 8 | # -d flag check for the directories 9 | # -s flag check file whether empty or not 10 | 11 | then 12 | echo "$file_name already exist" 13 | else 14 | echo "$file_name not found" 15 | fi 16 | 17 | #/*/*/*/*/*/**/**/**/*/*/*/*/*/*/*/*/*****/*/*/*/*/* 18 | 19 | echo -e "Enter the name of the file: \c" 20 | read file_name 21 | 22 | if [ -f $file_name ] 23 | then 24 | if [ -w $file_name ] 25 | then 26 | echo "Type some text data. To quit press ctrl+d" 27 | cat >> $file_name 28 | else 29 | echo "You do not have write permission to this file" 30 | fi 31 | else 32 | echo "$file_name not exist" 33 | fi -------------------------------------------------------------------------------- /integer_comparision.md: -------------------------------------------------------------------------------- 1 | */*/*/*/*/integer comparision */*/*/*/*/*/ 2 | -eq -is equal to if [ "$a" -eq "$b" ] 3 | -ne -is not equal to if [ "$a" -ne "$b" ] 4 | -gt -is greater than if [ "$a" -gt "$b" ] 5 | -ge -is gretater than or equal to if [ "$a" -ge "$b" ] 6 | -lt -is less than if [ "$a" -lt "$b" ] 7 | -le -is less than or equal to if [ "$a" -le "$b" ] 8 | < is less than if (("$a" < "$b")) 9 | <= is less than or equal to if (("$a" <= "$b")) 10 | > is greater than if (("$a" > "$b")) 11 | >= is greater than or equal to if (("$a" >= "$b")) 12 | 13 | */*/*/*/*/string comparision */*/*/*/*/*/ 14 | = is equal to if ["$a" = "$b"] 15 | == is equal to if ["$a" == "$b"] 16 | != is not equal to if ["$a" != "$b"] 17 | < is less than if ["$a" < "$b"] 18 | > is gretater than if ["$a" > "$b"] 19 | -z string is null, that is has zero lenght -------------------------------------------------------------------------------- /bash.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | ############################################################################## 3 | # SHORTCUTS and HISTORY 4 | ############################################################################## 5 | 6 | CTRL+A # move to beginning of line 7 | CTRL+B # moves backward one character 8 | CTRL+C # halts the current command 9 | CTRL+D # deletes one character backward or logs out of current session, similar to exit 10 | CTRL+E # moves to end of line 11 | CTRL+F # moves forward one character 12 | CTRL+G # aborts the current editing command and ring the terminal bell 13 | CTRL+H # deletes one character under cursor (same as DELETE) 14 | CTRL+J # same as RETURN 15 | CTRL+K # deletes (kill) forward to end of line 16 | CTRL+L # clears screen and redisplay the line 17 | CTRL+M # same as RETURN 18 | CTRL+N # next line in command history 19 | CTRL+O # same as RETURN, then displays next line in history file 20 | CTRL+P # previous line in command history 21 | CTRL+R # searches backward 22 | CTRL+S # searches forward 23 | CTRL+T # transposes two characters 24 | CTRL+U # kills backward from point to the beginning of line 25 | CTRL+V # makes the next character typed verbatim 26 | CTRL+W # kills the word behind the cursor 27 | CTRL+X # lists the possible filename completions of the current word 28 | CTRL+Y # retrieves (yank) last item killed 29 | CTRL+Z # stops the current command, resume with fg in the foreground or bg in the background 30 | 31 | ALT+B # moves backward one word 32 | ALT+D # deletes next word 33 | ALT+F # moves forward one word 34 | ALT+H # deletes one character backward 35 | 36 | BACKSPACE # deletes one character backward 37 | DELETE # deletes one character under cursor 38 | 39 | history # shows command line history 40 | !! # repeats the last command 41 | ! # refers to command line 'n' 42 | ! # refers to command starting with 'string' 43 | 44 | exit # logs out of current session 45 | 46 | 47 | ############################################################################## 48 | # BASH BASICS 49 | ############################################################################## 50 | 51 | env # displays all environment variables 52 | 53 | echo $SHELL # displays the shell you're using 54 | echo $BASH_VERSION # displays bash version 55 | 56 | bash # if you want to use bash (type exit to go back to your previously opened shell) 57 | whereis bash # locates the binary, source and manual-page for a command 58 | which bash # finds out which program is executed as 'bash' (default: /bin/bash, can change across environments) 59 | 60 | clear # clears content on window (hide displayed lines) 61 | 62 | 63 | ############################################################################## 64 | # FILE COMMANDS 65 | ############################################################################## 66 | 67 | 68 | ls # lists your files in current directory, ls to print files in a specific directory 69 | ls -l # lists your files in 'long format', which contains the exact size of the file, who owns the file and who has the right to look at it, and when it was last modified 70 | ls -a # lists all files in 'long format', including hidden files (name beginning with '.') 71 | ln -s # creates symbolic link to file 72 | touch # creates or updates (edit) your file 73 | cat # prints file raw content (will not be interpreted) 74 | any_command > # '>' is used to perform redirections, it will set any_command's stdout to file instead of "real stdout" (generally /dev/stdout) 75 | more # shows the first part of a file (move with space and type q to quit) 76 | head # outputs the first lines of file (default: 10 lines) 77 | tail # outputs the last lines of file (useful with -f option) (default: 10 lines) 78 | vim # opens a file in VIM (VI iMproved) text editor, will create it if it doesn't exist 79 | mv # moves a file to destination, behavior will change based on 'dest' type (dir: file is placed into dir; file: file will replace dest (tip: useful for renaming)) 80 | cp # copies a file 81 | rm # removes a file 82 | find . -name # searches for a file or a directory in the current directory and all its sub-directories by its name 83 | diff # compares files, and shows where they differ 84 | wc # tells you how many lines, words and characters there are in a file. Use -lwc (lines, word, character) to ouput only 1 of those informations 85 | sort # sorts the contents of a text file line by line in alphabetical order, use -n for numeric sort and -r for reversing order. 86 | sort -t -k # sorts the contents on specific sort key field starting from 1, using the field separator t. 87 | chmod -options # lets you change the read, write, and execute permissions on your files (more infos: SUID, GUID) 88 | gzip # compresses files using gzip algorithm 89 | gunzip # uncompresses files compressed by gzip 90 | gzcat # lets you look at gzipped file without actually having to gunzip it 91 | lpr # prints the file 92 | lpq # checks out the printer queue 93 | lprm # removes something from the printer queue 94 | genscript # converts plain text files into postscript for printing and gives you some options for formatting 95 | dvips # prints .dvi files (i.e. files produced by LaTeX) 96 | grep # looks for the string in the files 97 | grep -r # search recursively for pattern in directory 98 | 99 | 100 | ############################################################################## 101 | # DIRECTORY COMMANDS 102 | ############################################################################## 103 | 104 | 105 | mkdir # makes a new directory 106 | rmdir # remove an empty directory 107 | rmdir -rf # remove a non-empty directory 108 | mv # rename a directory from to 109 | cd # changes to home 110 | cd .. # changes to the parent directory 111 | cd # changes directory 112 | cp -r # copy into including sub-directories 113 | pwd # tells you where you currently are 114 | 115 | 116 | ############################################################################## 117 | # SSH, SYSTEM INFO & NETWORK COMMANDS 118 | ############################################################################## 119 | 120 | 121 | ssh user@host # connects to host as user 122 | ssh -p user@host # connects to host on specified port as user 123 | ssh-copy-id user@host # adds your ssh key to host for user to enable a keyed or passwordless login 124 | 125 | whoami # returns your username 126 | passwd # lets you change your password 127 | quota -v # shows what your disk quota is 128 | date # shows the current date and time 129 | cal # shows the month's calendar 130 | uptime # shows current uptime 131 | w # displays whois online 132 | finger # displays information about user 133 | uname -a # shows kernel information 134 | man # shows the manual for specified command 135 | df # shows disk usage 136 | du # shows the disk usage of the files and directories in filename (du -s give only a total) 137 | last # lists your last logins 138 | ps -u yourusername # lists your processes 139 | kill # kills the processes with the ID you gave 140 | killall # kill all processes with the name 141 | top # displays your currently active processes 142 | lsof # lists open files 143 | bg # lists stopped or background jobs ; resume a stopped job in the background 144 | fg # brings the most recent job in the foreground 145 | fg # brings job to the foreground 146 | 147 | ping # pings host and outputs results 148 | whois # gets whois information for domain 149 | dig # gets DNS information for domain 150 | dig -x # reverses lookup host 151 | wget # downloads file 152 | 153 | 154 | ############################################################################## 155 | # VARIABLES 156 | ############################################################################## 157 | 158 | 159 | varname=value # defines a variable 160 | varname=value command # defines a variable to be in the environment of a particular subprocess 161 | echo $varname # checks a variable's value 162 | echo $$ # prints process ID of the current shell 163 | echo $! # prints process ID of the most recently invoked background job 164 | echo $? # displays the exit status of the last command 165 | read # reads a string from the input and assigns it to a variable 166 | let = # performs mathematical calculation using operators like +, -, *, /, % 167 | export VARNAME=value # defines an environment variable (will be available in subprocesses) 168 | 169 | array[0]=valA # how to define an array 170 | array[1]=valB 171 | array[2]=valC 172 | array=([2]=valC [0]=valA [1]=valB) # another way 173 | array=(valA valB valC) # and another 174 | 175 | ${array[i]} # displays array's value for this index. If no index is supplied, array element 0 is assumed 176 | ${#array[i]} # to find out the length of any element in the array 177 | ${#array[@]} # to find out how many values there are in the array 178 | 179 | declare -a # the variables are treated as arrays 180 | declare -f # uses function names only 181 | declare -F # displays function names without definitions 182 | declare -i # the variables are treated as integers 183 | declare -r # makes the variables read-only 184 | declare -x # marks the variables for export via the environment 185 | 186 | ${varname:-word} # if varname exists and isn't null, return its value; otherwise return word 187 | ${varname:=word} # if varname exists and isn't null, return its value; otherwise set it word and then return its value 188 | ${varname:?message} # if varname exists and isn't null, return its value; otherwise print varname, followed by message and abort the current command or script 189 | ${varname:+word} # if varname exists and isn't null, return word; otherwise return null 190 | ${varname:offset:length} # performs substring expansion. It returns the substring of $varname starting at offset and up to length characters 191 | 192 | ${variable#pattern} # if the pattern matches the beginning of the variable's value, delete the shortest part that matches and return the rest 193 | ${variable##pattern} # if the pattern matches the beginning of the variable's value, delete the longest part that matches and return the rest 194 | ${variable%pattern} # if the pattern matches the end of the variable's value, delete the shortest part that matches and return the rest 195 | ${variable%%pattern} # if the pattern matches the end of the variable's value, delete the longest part that matches and return the rest 196 | ${variable/pattern/string} # the longest match to pattern in variable is replaced by string. Only the first match is replaced 197 | ${variable//pattern/string} # the longest match to pattern in variable is replaced by string. All matches are replaced 198 | 199 | ${#varname} # returns the length of the value of the variable as a character string 200 | 201 | *(patternlist) # matches zero or more occurrences of the given patterns 202 | +(patternlist) # matches one or more occurrences of the given patterns 203 | ?(patternlist) # matches zero or one occurrence of the given patterns 204 | @(patternlist) # matches exactly one of the given patterns 205 | !(patternlist) # matches anything except one of the given patterns 206 | 207 | $(UNIX command) # command substitution: runs the command and returns standard output 208 | 209 | 210 | ############################################################################## 211 | # FUNCTIONS 212 | ############################################################################## 213 | 214 | 215 | # The function refers to passed arguments by position (as if they were positional parameters), that is, $1, $2, and so forth. 216 | # $@ is equal to "$1" "$2"... "$N", where N is the number of positional parameters. $# holds the number of positional parameters. 217 | 218 | 219 | function functname() { 220 | shell commands 221 | } 222 | 223 | unset -f functname # deletes a function definition 224 | declare -f # displays all defined functions in your login session 225 | 226 | 227 | ############################################################################## 228 | # FLOW CONTROLS 229 | ############################################################################## 230 | 231 | 232 | statement1 && statement2 # and operator 233 | statement1 || statement2 # or operator 234 | 235 | -a # and operator inside a test conditional expression 236 | -o # or operator inside a test conditional expression 237 | 238 | # STRINGS 239 | 240 | str1 == str2 # str1 matches str2 241 | str1 != str2 # str1 does not match str2 242 | str1 < str2 # str1 is less than str2 (alphabetically) 243 | str1 > str2 # str1 is greater than str2 (alphabetically) 244 | str1 \> str2 # str1 is sorted after str2 245 | str1 \< str2 # str1 is sorted before str2 246 | -n str1 # str1 is not null (has length greater than 0) 247 | -z str1 # str1 is null (has length 0) 248 | 249 | # FILES 250 | 251 | -a file # file exists or its compilation is successful 252 | -d file # file exists and is a directory 253 | -e file # file exists; same -a 254 | -f file # file exists and is a regular file (i.e., not a directory or other special type of file) 255 | -r file # you have read permission 256 | -s file # file exists and is not empty 257 | -w file # your have write permission 258 | -x file # you have execute permission on file, or directory search permission if it is a directory 259 | -N file # file was modified since it was last read 260 | -O file # you own file 261 | -G file # file's group ID matches yours (or one of yours, if you are in multiple groups) 262 | file1 -nt file2 # file1 is newer than file2 263 | file1 -ot file2 # file1 is older than file2 264 | 265 | # NUMBERS 266 | 267 | -lt # less than 268 | -le # less than or equal 269 | -eq # equal 270 | -ge # greater than or equal 271 | -gt # greater than 272 | -ne # not equal 273 | 274 | if condition 275 | then 276 | statements 277 | [elif condition 278 | then statements...] 279 | [else 280 | statements] 281 | fi 282 | 283 | for x in {1..10} 284 | do 285 | statements 286 | done 287 | 288 | for name [in list] 289 | do 290 | statements that can use $name 291 | done 292 | 293 | for (( initialisation ; ending condition ; update )) 294 | do 295 | statements... 296 | done 297 | 298 | case expression in 299 | pattern1 ) 300 | statements ;; 301 | pattern2 ) 302 | statements ;; 303 | esac 304 | 305 | select name [in list] 306 | do 307 | statements that can use $name 308 | done 309 | 310 | while condition; do 311 | statements 312 | done 313 | 314 | until condition; do 315 | statements 316 | done 317 | 318 | ############################################################################## 319 | # COMMAND-LINE PROCESSING CYCLE 320 | ############################################################################## 321 | 322 | 323 | # The default order for command lookup is functions, followed by built-ins, with scripts and executables last. 324 | # There are three built-ins that you can use to override this order: `command`, `builtin` and `enable`. 325 | 326 | command # removes alias and function lookup. Only built-ins and commands found in the search path are executed 327 | builtin # looks up only built-in commands, ignoring functions and commands found in PATH 328 | enable # enables and disables shell built-ins 329 | 330 | eval # takes arguments and run them through the command-line processing steps all over again 331 | 332 | 333 | ############################################################################## 334 | # INPUT/OUTPUT REDIRECTORS 335 | ############################################################################## 336 | 337 | 338 | cmd1|cmd2 # pipe; takes standard output of cmd1 as standard input to cmd2 339 | < file # takes standard input from file 340 | > file # directs standard output to file 341 | >> file # directs standard output to file; append to file if it already exists 342 | >|file # forces standard output to file even if noclobber is set 343 | n>|file # forces output to file from file descriptor n even if noclobber is set 344 | <> file # uses file as both standard input and standard output 345 | n<>file # uses file as both input and output for file descriptor n 346 | n>file # directs file descriptor n to file 347 | n>file # directs file description n to file; append to file if it already exists 349 | n>& # duplicates standard output to file descriptor n 350 | n<& # duplicates standard input from file descriptor n 351 | n>&m # file descriptor n is made to be a copy of the output file descriptor 352 | n<&m # file descriptor n is made to be a copy of the input file descriptor 353 | &>file # directs standard output and standard error to file 354 | <&- # closes the standard input 355 | >&- # closes the standard output 356 | n>&- # closes the ouput from file descriptor n 357 | n<&- # closes the input from file descripor n 358 | 359 | 360 | ############################################################################## 361 | # PROCESS HANDLING 362 | ############################################################################## 363 | 364 | 365 | # To suspend a job, type CTRL+Z while it is running. You can also suspend a job with CTRL+Y. 366 | # This is slightly different from CTRL+Z in that the process is only stopped when it attempts to read input from terminal. 367 | # Of course, to interrupt a job, type CTRL+C. 368 | 369 | myCommand & # runs job in the background and prompts back the shell 370 | 371 | jobs # lists all jobs (use with -l to see associated PID) 372 | 373 | fg # brings a background job into the foreground 374 | fg %+ # brings most recently invoked background job 375 | fg %- # brings second most recently invoked background job 376 | fg %N # brings job number N 377 | fg %string # brings job whose command begins with string 378 | fg %?string # brings job whose command contains string 379 | 380 | kill -l # returns a list of all signals on the system, by name and number 381 | kill PID # terminates process with specified PID 382 | kill -s SIGKILL 4500 # sends a signal to force or terminate the process 383 | kill -15 913 # Ending PID 913 process with signal 15 (TERM) 384 | kill %1 # Where %1 is the number of job as read from 'jobs' command. 385 | 386 | ps # prints a line of information about the current running login shell and any processes running under it 387 | ps -a # selects all processes with a tty except session leaders 388 | 389 | trap cmd sig1 sig2 # executes a command when a signal is received by the script 390 | trap "" sig1 sig2 # ignores that signals 391 | trap - sig1 sig2 # resets the action taken when the signal is received to the default 392 | 393 | disown # removes the process from the list of jobs 394 | 395 | wait # waits until all background jobs have finished 396 | 397 | 398 | ############################################################################## 399 | # TIPS & TRICKS 400 | ############################################################################## 401 | 402 | 403 | # set an alias 404 | cd; nano .bash_profile 405 | > alias gentlenode='ssh admin@gentlenode.com -p 3404' # add your alias in .bash_profile 406 | 407 | # to quickly go to a specific directory 408 | cd; nano .bashrc 409 | > shopt -s cdable_vars 410 | > export websites="/Users/mac/Documents/websites" 411 | 412 | source .bashrc 413 | cd $websites 414 | 415 | 416 | ############################################################################## 417 | # DEBUGGING SHELL PROGRAMS 418 | ############################################################################## 419 | 420 | 421 | bash -n scriptname # don't run commands; check for syntax errors only 422 | set -o noexec # alternative (set option in script) 423 | 424 | bash -v scriptname # echo commands before running them 425 | set -o verbose # alternative (set option in script) 426 | 427 | bash -x scriptname # echo commands after command-line processing 428 | set -o xtrace # alternative (set option in script) 429 | 430 | trap 'echo $varname' EXIT # useful when you want to print out the values of variables at the point that your script exits 431 | 432 | function errtrap { 433 | es=$? 434 | echo "ERROR line $1: Command exited with status $es." 435 | } 436 | 437 | trap 'errtrap $LINENO' ERR # is run whenever a command in the surrounding script or function exits with non-zero status 438 | 439 | function dbgtrap { 440 | echo "badvar is $badvar" 441 | } 442 | 443 | trap dbgtrap DEBUG # causes the trap code to be executed before every statement in a function or script 444 | # ...section of code in which the problem occurs... 445 | trap - DEBUG # turn off the DEBUG trap 446 | 447 | function returntrap { 448 | echo "A return occurred" 449 | } 450 | 451 | trap returntrap RETURN # is executed each time a shell function or a script executed with the . or source commands finishes executing 452 | 453 | ############################################################################## 454 | # COLORS AND BACKGROUNDS 455 | ############################################################################## 456 | 457 | # Reset 458 | Color_Off='\033[0m' # Text Reset 459 | 460 | # Regular Colors 461 | Black='\033[0;30m' # Black 462 | Red='\033[0;31m' # Red 463 | Green='\033[0;32m' # Green 464 | Yellow='\033[0;33m' # Yellow 465 | Blue='\033[0;34m' # Blue 466 | Purple='\033[0;35m' # Purple 467 | Cyan='\033[0;36m' # Cyan 468 | White='\033[0;97m' # White 469 | 470 | # Additional colors 471 | LGrey='\033[0;37m' # Ligth Gray 472 | DGrey='\033[0;90m' # Dark Gray 473 | LRed='\033[0;91m' # Ligth Red 474 | LGreen='\033[0;92m' # Ligth Green 475 | LYellow='\033[0;93m'# Ligth Yellow 476 | LBlue='\033[0;94m' # Ligth Blue 477 | LPurple='\033[0;95m'# Light Purple 478 | LCyan='\033[0;96m' # Ligth Cyan 479 | 480 | 481 | # Bold 482 | BBlack='\033[1;30m' # Black 483 | BRed='\033[1;31m' # Red 484 | BGreen='\033[1;32m' # Green 485 | BYellow='\033[1;33m'# Yellow 486 | BBlue='\033[1;34m' # Blue 487 | BPurple='\033[1;35m'# Purple 488 | BCyan='\033[1;36m' # Cyan 489 | BWhite='\033[1;37m' # White 490 | 491 | # Underline 492 | UBlack='\033[4;30m' # Black 493 | URed='\033[4;31m' # Red 494 | UGreen='\033[4;32m' # Green 495 | UYellow='\033[4;33m'# Yellow 496 | UBlue='\033[4;34m' # Blue 497 | UPurple='\033[4;35m'# Purple 498 | UCyan='\033[4;36m' # Cyan 499 | UWhite='\033[4;37m' # White 500 | 501 | # Background 502 | On_Black='\033[40m' # Black 503 | On_Red='\033[41m' # Red 504 | On_Green='\033[42m' # Green 505 | On_Yellow='\033[43m'# Yellow 506 | On_Blue='\033[44m' # Blue 507 | On_Purple='\033[45m'# Purple 508 | On_Cyan='\033[46m' # Cyan 509 | On_White='\033[47m' # White 510 | 511 | # Example of usage 512 | echo -e "${Green}This is GREEN text${Color_Off} and normal text" 513 | echo -e "${Red}${On_White}This is Red test on White background${Color_Off}" 514 | # option -e is mandatory, it enable interpretation of backslash escapes --------------------------------------------------------------------------------