sirgatez

Untitled

Apr 3rd, 2023 (edited)
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.50 KB | Source Code | 0 0
  1. GenerateAliasesAndFunctions.sh
  2.  
  3. #!/bin/zsh
  4.  
  5. # To use: source <thisfile>
  6.  
  7. # I keep this first like in a file called GoLinks_pre.sh which loads first
  8. export go_hardlinks=()
  9. export go_mylinks=()
  10. export go_searchlinks=()
  11.  
  12. # This is kept in Golinks.sh which loads second
  13.  
  14. # LinkedIn Go Links go/<thing>
  15. # Note these are provided for example only and will not work outside of the LinkedIn internal network
  16. # Now you could setup your own DNS or URL redirect for "go" and redirect it to whatever you wanted. For example bit.ly links.
  17. go_hardlinks=(${go_hardlinks[@]}
  18.     'helpin' 'questions' 'wiki' # Research
  19. )
  20.  
  21. # <thing>
  22. go_mylinks=(${go_mylinks[@]}
  23.     # Web tools
  24.     'gmail:https://mail.google.com'
  25.  
  26.     # Conversions and Experimentation
  27.     'epochconverter:https://www.epochconverter.com'
  28.     'jsfiddle:https://jsfiddle.net'
  29.     'regex101:https://regex101.com'
  30.     'urlencoder:https://meyerweb.com/eric/tools/dencoder'
  31.     'keycodes:https://keycode.info'
  32.     'keycodechart:http://www.foreui.com/articles/Key_Code_Table.htm'
  33. )
  34.  
  35. # <thing> [params]
  36. go_searchlinks=($go_searchlinks[@]
  37.     # Shopping
  38.     'amazon:https://www.amazon.com###/s?url=search-alias%3Daps&field-keywords=$$$'
  39.  
  40.     # Web Searches
  41.     'google:https://www.google.com###/search?q=$$$'
  42.     'google_img:https://www.google.com###/search?q=$$$&tbm=isch'
  43.  
  44.     # Fun
  45.     'httpcat:https://http.cat/###$$$'
  46.  
  47.     # Research
  48.     'colorhex:https://www.color-hex.com###/color/$$$'
  49.     'macapp:http://macappstore.org/###$$$/'
  50.     'nasdaq:https://www.nasdaq.com###/symbol/$$$/real-time'
  51.     'rfc:https://tools.ietf.org###/html/$$$'
  52.     'stackoverflow:https://stackoverflow.com###/search?q=$$$'
  53. )
  54.  
  55. function _golinks_setup() {
  56.     local go_links app link go_l f
  57.     go_links=(${go_hardlinks[@]} ${go_mylinks[@]} ${go_searchlinks[@]})
  58.     for go_l in ${go_links}; do
  59.         if [[ "a${go_l}" =~ ":" ]]; then
  60.             if [[ "a${go_l}" =~ '\$\$\$' ]]; then
  61.                 go_l=${${go_l//\\&/&}//&/\\&} # Escape ambersand, as it expands unintentionally in functions.
  62.                 app=$(echo "${${${go_l}%%:*}}") # Bash subsitution for "cut -f1 -d:"
  63.                 link=$(echo "${${go_l}#*:}")    # Bash subsitution for "cut -f2- -d:"
  64.                 # Note the regex requires 4x$ for a match of 3x. Otherwise we keep 1 $.
  65.                 # Define function template and static variables
  66.                 if [[ "a${go_l}" =~ '###' ]]; then
  67.                     f='function ${app}() { if [[ $[#] -eq 0 ]]; then url "${link_s}";else;url "${link}";fi }' # Template function
  68.                     f=$(echo "${f}" | sed 's@${link_s}@'"${link%###*}"'@' | sed 's@${link}@'"${link//\#\#\#/}"'@')
  69.                 else
  70.                     f='function ${app}() { if [[ $[#] -eq 0 ]]; then echo "Error: $0 requires at least 1 argument (search term)";return 1;fi;url "${link}" }' # Template function
  71.                 fi
  72.                 echo $(echo $f | sed 's@${app}@'"${app}"'@' | sed 's@${link}@'"${link//\#\#\#/}"'@' | sed 's/\$\$\$/$(urlencode "${@}")/')  >> GenerateAliasesAndFunctions_Generated.sh
  73.             else
  74.                 app=$(echo "${${${go_l}%%:*}}")
  75.                 link=$(echo "${${go_l}#*:}")
  76.                 echo alias ${app}="'url \"${link}\"'"  >> GenerateAliasesAndFunctions_Generated.sh # Define custom alias
  77.             fi
  78.         else
  79.             echo alias go/${go_l}="'url \"http://go/${go_l}\"'" >> GenerateAliasesAndFunctions_Generated.sh # Define simple alias. Only works at Google and LinkedIn
  80.         fi
  81.     done
  82. }
  83.  
  84. _golinks_setup
Add Comment
Please, Sign In to add comment