Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tips
- tips () {
- red='\033[01;31m'
- green='\033[01;32m'
- NC='\033[0m' # No Color
- #List all tip
- if [[ $# -eq 0 ]]; then
- clear
- echo -e "${red}Usage: tips TIP-NAME${NC}"
- echo -e
- echo -e "${green}random${NC}: How to generate a random number of a certain length
- ${green}para${NC}: Parameter expansion
- ${green}hist${NC}: History
- ${green}var${NC}: Bash Internal Variables
- ${green}test${NC}: Shell tests
- ${green}mov${NC} : Efficient Cursor Movements
- ${green}array${NC} : How to work with arrays
- ${green}short${NC} : Shortcut"| sort
- fi
- #List specific tip
- case $1 in
- random)
- clear
- echo -e "${red}To get random number from range:${NC}"
- echo -e "${green}shuf -i LOW-HIGH -n 1${NC}"
- echo -e
- ;;
- mov)
- clear
- echo -e "${red}Moving to the beginning and end of line${NC}"
- echo -e "${green}^a${NC}: To beginning of the line (bol)."
- echo -e "${green}^e${NC}: To end of line (eol)."
- echo -e
- echo -e "${red}Moving a character or word at a time${NC}"
- echo -e "${green}^f${NC}: Forward one character."
- echo -e "${green}^b${NC}: Backward one character."
- echo -e "${green}alt+f${NC}: Move cursor forward one word."
- echo -e "${green}alt+b${NC}: Move cursor backward one word."
- echo -e
- echo -e "${red}Transposing letters or words${NC}"
- echo -e "${green}^t${NC}: Switches the character with the one next to it."
- echo -e "${green}alt+t${NC}: transpose the word at the cursor location with the one preceding it."
- echo -e
- echo -e "${red}Changing casing${NC}"
- echo -e "${green}alt+l${NC}: Convert characters from cursor to end of the word to lowercase."
- echo -e "${green}alt+u${NC}: Convert characters from cursor to end of the word to uppercase."
- echo -e
- echo -e "${red}Deleting characters or words${NC}"
- echo -e "${green}^d${NC}: Delete the character at the cursor location."
- echo -e "${green}^k${NC}: Kill the text from the cursor location to eol."""
- echo -e "${green}^u${NC}: Kill text from the cursor location to bol."
- echo -e "${green}alt+d${NC}: Kill text from the cursor location to the end of the current word."
- echo -e
- echo -e "${red}Clearing the terminal window${NC}"
- echo -e "${green}^l${NC}: Alternative to the command ${green}clear${NC}."
- echo -e
- ;;
- para)
- clear
- x=abcdefghifirstjklmnopwordqrstuvwxuz0first1
- echo -e ${green}'$x'${NC}: X: $x
- echo -e
- echo -e "${red}Case 1${NC}:"
- echo -e ${green}'${x^}'${NC}: Uppercase first letter ${x^}
- echo -e ${green}'${x^^}'${NC}: Uppercase all letters ${x^^}
- echo -e
- echo -e "${red}Substrings…${NC}:"
- echo -e ${green}'${x:12}'${NC}: First 12: ${x:12}
- echo -e ${green}'${x:15:10}'${NC}: 15 for 10: ${x:15:10}
- echo -e
- echo -e "${red}Middle Substrings…${NC}:"
- echo -e ${green}'mid=${x#*o} mid=${mid%o*}'${NC}: Shows what"'"s between the two "'o'": $x "->" ${x#*o} "->" ${mid%o*}
- echo -e
- echo -e "${red}Removals…${NC}"
- echo -e ${green}'${x#*o}'${NC}: everything to o ${x#*o}
- echo -e ${green}'${x##*o}'${NC}: everything to last o ${x##*o}
- echo -e ${green}'${x%o*}'${NC}: everything from back to o ${x%o*}
- echo -e ${green}'${x%%o*}'${NC}: everything from back to last o ${x%%o*}
- echo -e
- echo -e "${red}Replacements…${NC}"
- echo -e ${green}'${x/first/this}'${NC}: replace first occurrence of word ${x/first/this}
- echo -e ${green}'${x//first/this}'${NC}: replace all occurrence of word ${x//first/this}
- echo -e ${green}'${x/first}'${NC}: if no replacement given replace once ${x/first}
- echo -e ${green}'${x//first}'${NC}: if no replacement given replace all ${x//first}
- x=ABCDEFGHIFIRSTJKLMNOPWORDQRSTUVWXUZ0FIRST1
- echo -e
- echo -e "${red}Case 2${NC}"
- echo -e ${green}'$x'${NC}: X: $x
- echo -e
- echo -e ${green}'${x,}'${NC}: Lowercase first letter ${x,}
- echo -e ${green}'${x,,}'${NC}: Lowercase all letters ${x,,}
- echo -e ${green}'${x~}'${NC}: Lowercase first letter ${x~}
- echo -e ${green}'${x~~}'${NC}: Lowercase all letters ${x~~}
- y=abababcacaca
- var="${y//[^a]}"
- echo -e
- echo -e "${red}Count how many time a string appear${NC}"
- echo -e
- echo -e ${green}'$y'${NC}: X: $y
- echo -e
- echo -e ${green}'${y//[^a]}'${NC}: Extract the string 'a' from the whole 'abababcacaca', in this case: ${y//[^a]}
- echo -e ${green}'var="${y//[^a]}" && echo "${#var}"'${NC}: The proper count of how many 'a' there is: ${#var}
- echo -e
- ;;
- hist)
- clear
- echo -e "${red}History${NC}"
- echo -e "${green}!51${NC}: Recall and execute the command associated with the history number 51."
- echo -e "${green}!-2${NC}: Recall and execute a command that we typed before our most recent one."
- echo -e "${green}CTRL-p${NC}: Recall and execute the last command."
- echo -e "${green}!!${NC}: Recall and execute the last command '(shortcut to ${green}!-1${NC}.)'."
- echo -e "${green}sudo !!${NC}: Sudo the last command."
- echo -e "${green}^foo^bar^${NC}: Replace ${green}'foo'${NC} by ${green}'bar'${NC} in the last command."
- echo -e "${green}!!:1${NC}: Refers to the first argument of the last command."
- echo -e "${green}!!:2-4${NC}: Refers to the 2nd to 4th argument of the last command."
- echo -e "${green}!!:^${NC}: Refers to the first argument of the last command."
- echo -e "${green}!!:\$${NC}: Refers to the last argument of the last command."
- echo -e "${green}CTRL-r${NC}: Search the history forward."
- echo -e "${green}CTRL-s${NC}: Search history backward."
- echo -e "${green}ALT+.${NC}: Place the argument of the most recent command on the shell" # http://www.commandlinefu.com/commands/view/1551/place-the-argument-of-the-most-recent-command-on-the-shell
- echo -e
- echo -e "${red}Modifiers${NC}"
- echo -e "${green}!!:$:r${NC}: Removes extension of the last argument of the last command."
- echo -e "${green}!!:$:e${NC}: Removes all but the extension of the last argument of the last command."
- echo -e "${green}!!:$:h${NC}: Removes the trailing pathname component, leaving the head '"/usr/local/apache" to "/usr/local"'."
- echo -e
- ;;
- test)
- clear
- echo -e "${red}Test if a variable is divisible by a number${NC}"
- echo -e 'if ! ((n % 4)); then'
- echo -e ' echo "$n divisible by 4."'
- echo -e 'fi'
- echo -e
- echo -e "${red}Test the number of arguments${NC}"
- echo -e 'if [ "$#" -eq 2 ]; then'
- echo -e ' echo "The arguments are 2 as they should be"'
- echo -e 'fi'
- echo -e
- echo -e "${red}Test the exit status${NC}"
- echo -e 'if [[ "$?" != 0 ]]; then'
- echo -e ' echo "Exit status is dofferent from 0"'
- echo -e 'fi'
- echo -e
- ;;
- var)
- clear
- echo -e "${red}Bash Internal Variables${NC}"
- echo -e
- echo -e ${green}'$*'${NC}": Expands all the positional parameters (Bad for parameters with space)"
- echo -e '\t'\$ 'func() { for i in $*; do echo $i; done };'
- echo -e '\t'\$ 'func hi there "hi there"'
- echo -e '\thi'
- echo -e '\tthere'
- echo -e '\thi'
- echo -e '\tthere'
- echo -e
- echo -e ${green}'"$*"'${NC}": Expands all the positional parameters as ONE single string"
- echo -e '\t'\$ 'func() { for i in "$*"; do echo $i; done };'
- echo -e '\t'\$ 'func hi there "hi there"'
- echo -e '\thi there hi there'
- echo -e
- echo -e ${green}'$@'${NC}": Expands all the positional parameters (Bad for parameters with space)"
- echo -e '\t'\$ 'func() { for i in $@; do echo $i; done };'
- echo -e '\t'$ 'func hi there "hi there"'
- echo -e '\thi'
- echo -e '\tthere'
- echo -e '\thi'
- echo -e '\tthere'
- echo -e
- echo -e ${green}'"$@"'${NC}": Expands all the positional parameters and respects space (Good for parameters with space)"
- echo -e '\t'\$ 'func() { for i in "$@"; do echo $i; done };'
- echo -e '\t'\$ 'func hi there "hi there"'
- echo -e '\thi'
- echo -e '\tthere'
- echo -e '\thi there'
- echo -e
- echo -e ${green}'$#'${NC}": The number os positional parameters"
- echo -e
- echo -e ${green}'$?'${NC}": Exit code of the latest command in the foreground"
- echo -e
- echo -e ${green}'$!'${NC}": PID of the latest command in the background"
- echo -e
- echo -e ${green}'$$'${NC}": PID of the current shell (different for scripts but same for functions)"
- echo -e
- echo -e ${green}'$_'${NC}": Last argument of the last command"
- echo -e
- echo -e ${green}'$-'${NC}": Bash options (see xv())"
- echo -e
- ;;
- short)
- clear
- echo -e "${red}Shortcuts${NC}"
- echo -e
- echo -e "${green}CTRL-u...CTRL-y${NC}: Delete a whole command line, check another command if needed then yank it back."
- echo -e
- echo -e "${green}(cd /tmp && ls)${NC}: Jump to a directory, execute a command and jump back to current dir"
- echo -e
- ;;
- array)
- clear
- echo -e "${red}Set an array${NC}"
- echo -e '\t' array=\(one two three \"four five\"\)
- echo -e
- echo -e "${red}Show the array content${NC}"
- echo -e '\t' \$ echo \${array[@]}
- echo -e '\t' one two three four five
- echo -e "${red}Show the array indexes${NC}"
- echo -e '\t' \$ echo \${\!array[@]}
- echo -e '\t' 0 1 2 3
- echo -e "${red}Show the array length${NC}"
- echo -e '\t' \$ echo \${\#array[@]}
- echo -e '\t' 4
- echo -e "${red}Iterate depending on an array indexes${NC}"
- echo -e '\t' \$ for index in \"$\{\!array\[\@\]\}\"\; do echo \"\$\{array\[index\]\}\"\; done
- echo -e '\t' one
- echo -e '\t' two
- echo -e '\t' three
- echo -e '\t' four five
- echo -e
- echo -e ${green}check${NC} http://www.thegeekstuff.com/2010/06/bash-array-tutorial
- echo -e
- ;;
- esac
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement