Run command as another user

In two ways

  • su deployer -c 'whoami can add login -l option su -l deployer -c 'rvm list' so PATH is extended with ~/.rvm/bin/ and rvm script works
  • sudo -u deployer whoami simpler, no need for quotes
    • Add option -i or --login to run as login shell. In login shell ~ is pointing to /home/deployer/ not /root/ home.
    • Wrap command inside bash to properly load XDG_RUNTIME_DIR for rails c or other ruby commands: sudo -i -u deployer /bin/bash -c "cd /vagrant && rails c". Also when you use pipe | or && or redirection >>, you should wrap inside bash or use eval

Shortcuts for windows

Very usefull shortcuts to activate certain windows.

  • gnome-terminal if negative is used, than it is bottom alligned
  • wmctrl
  • xwininfo to click on window to find it’s 0x440003d which we can use to create shortcut xdotool windowactivate 0x440003d
  • another robust solution is to set predefined classname for windows with xprop -f WM_CLASS 8s -set WM_CLASS main_editor (click on window) and xdotool search --classname main_editor windowactivate in Alt+M shortcut. I’m using ALT+HJKL; with class_hjkl;

mapping hjkl keys to activate window mapping workspace keys to hjkl Look at my window_shortcuts

When I update ubuntu I need to map keys again. Go one by one and press ALT+key combination again.

On blank ubuntu I need to go All Settings -> Appearance -> (tab) Behavior and Auto-hide the Launcher to ON and Enable workspaces. (in virtualBox I can not open launcher, only hud with left alt)

Shortcuts for key mappings

To scroll in terminal window, instead shift+page_up we can bind to key simulate keyup. xbindkeys I usually map vim shortcuts CTRL+ , so for terminal windows I use ALT+. Do not override already two ALT bash shortcut Alt+F Alt+B to move cursor one word (use capitalize F to not open File menu or disable in Edit -> Keyboard Shortcuts) (CTRL version move by one character) . I also map ctrl+k to scroll up since it’s natural (I don’t mind that I don’t have cut shortcut, I just use #). I tried to use bind command but that outputs, so multiple scroll looks like one scroll.

Here are all bash CTRL shortcuts:

  • ctrl+a (+e) : move cursor to begging (end) of line
  • ctrl+b (+f) : move cursor by one char to left (right)
  • ctrl+h (+d) : delete char left to the cursor-backspace (under the cursor)
  • ctrl+w : delete word before the cursor
  • ctrl+i : (or tab) completion
  • ctrl+j : enter
  • ctrl+k (+u) [+y]: cut chars until the end of the line (before the cursor position) [yank cutted text]
  • ctrl+p (+n) : go to previous (next) command
  • ctrl+r : search
  • ctrl+c (+z) : kill current process (send to background and suspend, use fg to restore). You can put any shell (vim, rails s) to suspend state with Control + z. Than you can put it in background bg if needed. You can use that shell for inspection other things. When you are finished, you can switch back to vim, or rails s, with foreground fg (from both suspend and background state) see all background and suspended processes in current bash session jobs -l
  • ctrl+l : clear window, but I remaped to copy current line to clipboard

ALT shortcuts:

  • alt+b (+f) : move cursor by one word to left (right). On mac Alt is Meta so you need to disable in all your terminals in system preferences and restart.

Here are some Edit -> Keyboard Shortcuts:

  • shoft+ctrl+t : open in new tab
  • alt+1 : switch between tabs

To copy line, you need to select with mouse and use ctrl+shift+c (ctrl+shift+v to pase). Or with xsel you can bind to ctrl+l:

# .bashrc
bind '"\C-l": "\C-e\C-u xsel --clipboard <<"EOF"\n\C-y\nEOF\n\C-y"'

In vim you can paste primary selection (just select, middle mouse, shift insert) with "*p. Clipboard selection (copy, paste) with and "+p

Maximize terminal window to full screen is with F11 (yes it is the same shortcut for bash as for chrome). Inside terminal there are Terminal->Preferences->Shortcuts

  • Shift+Ctrl+F search (G and H next and previous)

Only place where I sometimes need arrow keys is google chrome suggestions in address bar, but than I use Tab.

Starting chrome with command line switch chromium-browser --auto-open-devtools-for-tabs does work only on google-chrome.

Intresting devtools-cheatcheet (search by filename Ctrl+O, search all sources Ctrl+Shift+F).

Look my xbindkeysrc There is very nice tool Xmonad which is designed to maximize. move windows around, automatically tile the screen without gaps or overlap, but I do not need auto arrangement.

To start command programs on startup you can use ctrontab -r and @reboot, but for gui programs use ubunt Startup Applications. Output (from crontab or gui) should be redirected to system log, for example

# script should start with #!/bin/bash
# and should be executable chmod +x my_script.sh
# in script use echo "start my script"
/home/orlovic/config/bashrc/startup_my_script.sh 2>&1 | /usr/bin/logger
# using tags works only if you call script direclty, but if it is called from
# startup scripts than tag is gnome-session it overrides my tag
# --tag my_tag
# sometimes messages are grouped so wait few seconds
tail -f /var/log/syslog
# clear syslog
> /var/log/syslog
# if syslog is not readable you can make it readable and writable
sudo chmod g+rw /var/log/syslog

You can also redirect output to a file withing the script itself (but redirecting to syslog does not work)

#!/bin/bash
exec >> /home/orlovic/Downloads/output.log
exec 2>&1

echo this will go to /home/orlovic/Downloads/output.log

For gui debugging you can use this command to log message from scripts: notify-send $USER --urgency critical Create desktop notifications with notify-send but need urgency critical, so I made alias alert. You can debug with sleep and echo redirection.

Since ctrl+w and ctrl+r in chrome firefox browsers will close and reload the page, it is much easier to use alt (like on mac), so I created shortcuts

To run script on keyboad shortcut go to Settings -> Keyboard -> Shortcuts -> Custom Schortcuts -> +

  • name: this is description
  • command: should be path to the executable script… Directly writting commands here might not work

If you are mapping Alt+W to send another key Ctrl+W you need to release Alt first. To release all modifiers you can use

#!/bin/bash
# config/bashrc/ctrl_browser.sh
# exec >> /home/orlovic/Downloads/output.log
# exec 2>&1
if [ "$1" = "" ]; then
  notify-send "Please provide a key, like Ctrl+r" -u critical
fi

# release all modifiers
# https://unix.stackexchange.com/questions/60007/how-to-force-release-of-a-keyboard-modifiers
xdotool keyup Shift_L Shift_R Control_L Control_R Meta_L Meta_R Alt_L Alt_R Super_L Super_R Hyper_L Hyper_R ISO_Level2_Latch ISO_Level3_Shift ISO_Level3_Latch ISO_Level3_Lock ISO_Level5_Shift ISO_Level5_Latch ISO_Level5_Lock
xdotool key $1
# keydown again so you can run multiple reloads while keeping alt key down
# problem is if you release alt to fast, you need to click again to release it
# xdotool keydown Alt_L

You can send commands to specific window https://unix.stackexchange.com/questions/87831/how-to-send-keystrokes-f5-from-terminal-to-a-gui-program

# this does not send key
xdotool search --classname vp_$(get_current_viewport)_class_slash  windowactivate --sync %1 key  a

# also can not send key without activate that window
xdotool key --window 67199662 a Return

# with windowactivate you need to wait with sync and than send key
xdotool windowactivate --sync 67199662 key a

# to write command use `type`, to include spaces wrap with quotes
xdotool windowactivate --sync 67199662 type "pwd"

# to send Enter <CR> you need delay and also some key before that
# <CR> is 'Return', space is 'space'
# remember not to run command with <C-j> since ctrl modifier is still active
xdotool windowactivate --sync 67199662 type "pwd"; xdotool key --delay 50 space Return

# you can come back to previous terminal with
xdotool windowactivate --sync 67199662 type "pwd"; xdotool key --delay 50 space Return windowactivate 67199574

# complete command for janko-m/vim-test strategy is in vim tips

Remapping keyboard keys is with xmodmap.

# show all keys, columns are
# Key, Shift+Key, mode_switch+Key, mode_switch+Shift+Key, alt+Key, alt+shift+key
xmodmap -pke

# save configuration to a file
xmodmap -pke > ~/.Xmodmap
# reload new configuration
xmodmap ~/.Xmodmap

To find keycodes use xev program (exit with mouse or alt+f4). showkeys is using for linux system. You can use also xkeycaps but I do not know which keyboard to choose. sudo showkey -a can show keycode.

xev | sed -n 's/^.*keycode *\([0-9]\+\).*$/keycode \1 = /p'

Keycode for a is 38. It can be used alone or with modifier keys. Alone a gives keysym 0x61 (ie 141 in ascii), for shift+a keysym is 0x41 ie A. Descriptive keysyms for multimedia keys are in /usr/include/X11/XF86keysym.h. Keycode for left ctrl is 37 (keysym Control_L), left alt 64 (keysym Alt_L), windows 133 (keysym Super_L), caps lock 66 (Caps_Lock), right alt is 108 (Alt_R).

http://wiki.linuxquestions.org/wiki/List_of_Keysyms_Recognised_by_Xmodmap http://wiki.linuxquestions.org/wiki/List_of_KeySyms AltGr, Alt_R, AltGr_R are synonyms.

From man xmodmap you can see that you can write 4 keysyms

Up to eight keysyms may be attached to a key, however the last four are not used in any major X server implementation. The first keysym is used when no modifier key is pressed in conjunction with this key, the second with Shift, the third when the Mode_switch key is used with this key and the fourth when both the Mode_switch and Shift keys are used

keycode 57 = key Shift+Key Mode_switch+Key Mode_switch+Shift+Key ISO_Level3_Shift+Key ISO_Level3_Shift+Shift+Key

Do not know where is ISO_Level3_Shift. Mode switch is on Caps_Lock. When I bind to for example number 1 and 2, than 13 gives 4 (so Mode_switch works) but 23 gives 3 (ISO_Level3_Shift does not work)

keycode  10 = Mode_switch exclam 1 exclam
keycode  11 = ISO_Level3_Shift at 2 at
keycode  12 = 3 numbersign 4 numbersign 5 6

Default output of xmodmap -pm is

xmodmap:  up to 4 keys per modifier, (keycodes in parentheses):

shift       Shift_L (0x32),  Shift_R (0x3e)
lock        Caps_Lock (0x42)
control     Control_L (0x25=37), Control_R (0x69)
mod1        Alt_L (0x40=64),  Alt_R (0x6c=108),  Meta_L (0xcd)
mod2        Num_Lock (0x4d)
mod3
mod4        Super_L (0x85),  Super_R (0x86),  Super_L (0xce),  Hyper_L (0xcf)
mod5        ISO_Level3_Shift (0x5c),  Mode_switch (0xcb)

To change modifier keys usually you should to clear them; Lock, Shift, Control, Mod1 (this is alt) … and than add Control = Super_L .... Modifier Mod4 is usually Super_L.

! This modifies CapsLock to Control, and keep CapsLock on Shift+CapsLock
clear lock
clear control
keycode 66 = Control_L Caps_Lock NoSymbol NoSymbol
add control = Caps_Lock Control_L Control_R

You can use dummy modifier on capslock and assign various key on that modifier. https://gist.github.com/bcremer/9caa54d0432531f80c1e

For Apple keyboard you can use https://help.ubuntu.com/community/AppleKeyboard and this answer https://askubuntu.com/questions/131900/how-do-i-switch-the-command-key-and-control-key-on-a-macbook-pro for swapping controll and command (I use swapping option and command). For swapping fn and control use https://help.ubuntu.com/community/TroubleWithAppleKbdOnUbuntu https://wiki.archlinux.org/index.php/Apple_Keyboard#Treating_Apple_keyboards_like_regular_keyboards https://github.com/free5lot/hid-apple-patched#installation-via-dkms-recommended

sudo vi /etc/modprobe.d/hid_apple.conf
cat /sys/module/hid_apple/parameters/swap_fn_leftctrl

I use mapping for all keys that I need right shift like underscore, brackets, html >.

! ~/.Xmodmap
! remove caps lock modifier
clear Lock
! assign dummy modifier to capslock
keycode 66 = ISO_Group_Shift ISO_Group_Shift ISO_First_Group NoSymbol
keycode 43 = h H braceleft
keycode 44 = j J parenleft
keycode 45 = k K parenright
keycode 46 = l L braceright
keycode 59 = comma less less
keycode 60 = period greater greater

clear Mod1
add Mod1 = Alt_L
keycode 108 = underscore underscore underscore underscore

! Colon and semicolon are just reversed.
keycode 47 = colon semicolon colon semicolon

Also I like command+c / alt+c for copy/paste so I use this answer to copy selected text to clipboard https://askubuntu.com/questions/573663/command-to-copy-currently-selected-text and in terminal remap copy paste Terminal->Preferences->Shortcuts->Edit->Copy (Alt+C) Paste (Alt+V) so I can create keyboard shortcut for Alt+C and Alt+V which I can use both on terminal and non terminal

# /home/orlovic/config/bashrc/copy_from_x_selection.sh
#!/bin/bash
xclip -out -selection primary | xclip -in -selection clipboard

create two shortcuts, one for Alt+C /home/orlovic/config/bashrc/copy_from_x_selection.sh and another for Alt+V /home/orlovic/config/bashrc/ctrl_browser.sh Control+v. Do not forget to use old method of selecting text and middle mouse button or Shift+Insert (Insert does not exists on Apple Keyboard so that is the reason why I add this shortcuts, but also another helpers is ctrl+i which is remaped to shift+insert in .xbindkeysrc). This xclip is also used when you want to copy from vim to clipboard, you need just to yank and alt+c and alt+v (alt+c do not need to be inside vim).

Run /home/orlovic/config/bashrc/startup_xmodmap.sh 2>&1 | /usr/bin/logger in both startup scripts and /lib/systemd/system-sleep/startup_after_sleep.sh

Problem with system-sleep xmodmap: unable to open display can not be solved with https://ubuntuforums.org/showthread.php?t=2380045

sleep 5
declare -x DISPLAY=":0.0"
declare -x XAUTHORITY="/home/<your user>/.Xauthority"

Maybe pm power manager is different than system sleep, but it needs some events to properly work. X server is not set in systemd script so we need to declare and export DISPLAY and XAUTHORITY. I success with xhost +main (enable connect from main, but since network manager is not yet initialized, that is not reliable). I also tried https://bbs.archlinux.org/viewtopic.php?pid=684936#p684936 so nm knows hostname in configuration

# /etc/NetworkManager/NetworkManager.conf
[keyfile]
hostname=main

but not reliable…

Tar

comress folder: tar -zcvf folder.tar.gz folder extract folder: tar -zxvf folder.tar.gz

compress file: tar cvzf file_name.tar.gz file_name Note that is file_name is relative to some subfolders (contains slash /) for example /tmp/db.dump than leading slash will be ignored and when you extract it will be in tmp/db.dump

Scrips and commands

  • nice tutorial Bash Begginers Guide and example config scrips
  • input first param $1. $@ is all params. Use "$@" is you pass to another script and you want to preserve quoted strings https://stackoverflow.com/questions/4824590/propagate-all-arguments-in-a-bash-shell-script So when variable contain spaces and you need to pass to a command than wrap around with quotes, for example A="file with space.txt"; ls "$A"
  • exit from script with exit 1
  • functions can accept default parameters with :- syntax
    myFunc() {
      echo first param is $(1:-not defined)
    }
    

    To use functions in xargs you need to export them

    myF() {
      echo $1
    }
    export -f myF
    
  • always use double square brackets [[ ]] instead of single [ ] since it is extension and support using || instead of -o, or regex match =~
  • with comparison operators you should use quotes since something could not be defined if [ -n "$a" ]. If you use double brackets you do not need quotes around variable name.

    # check if $a is present (not null)
    if [[ -n $a ]]
    
    # if conditional to check if string is substring
    string='my long string'
    if [[ $string = *"long"* ]]; then
      echo "its here"
    fi
    
    # string equal
    if [[ $a != $b ]]; then
      echo string a is not equal to string b
    fi
    
    # split string and use last item
    number="$(echo "asd 123" | cut -d' ' -f2)"
    
    # integer equal
    if [[ $a -eq $b ]]; then
      echo number a is equal to number b
    fi
    

    Find other test helpers in man test. Use conditional or || and and && and group () and negative !

    if [[ $a -eq $b || ($a -eq 2 && $b > 2) ]] || [[ ! -n $a ]] ; then
      echo number a is equal to number b
    fi
    

    Use regular expression matching (not supported with [ ] single quote)

    w_name="Mozzila Firefox and Chrome"
    if [[ $w_name =~ 'Mozilla Firefox'|Chrome ]] ; then
    fi
    
  • loops and expressions example

    attempts="0"
    while [ $attempts -lt 10 ] || [ -z "$window_id" ]
    do
      echo $attempts
      sleep 1
      window_id=`wmctrl -l | grep $url | awk '{print $1}' | tail -n1`
      attempts=$[$attempts+1]
    done
    echo move and mark window_id=$window_id
    
  • find
    • find and remove files find . -type f -name "test.log" -exec rm -f {} \;
    • find all html files find . -name '*.html'. Note that find . -name *.html will not work since wildcard will be changed to html file in current folder.
    • filter files based on their size: find . -size -212c show only small than 212 bytes
  • escape single quote ' in linux scripts with $'Hello I\'m here' link
  • add task to crontab with linux command # (crontab -l 2>/dev/null; echo "@reboot /home/orlovic/my.sh") | crontab -
  • output goes to the mail but I prefer to redirect to some file. Also define shell since it will use sh (on which sleep does not work). ~~~ SHELL=/bin/bash
            • /path/to/my/script.sh » /home/orlovic/Downloads/cron.log 2>&1 ~~~

    cron format Minute Hour Day_of_the_Month Month_of_the_Year Day_of_the_Week and also Year as 6th field. For more information see the manual pages of crontab(5) and cron(8) m h dom mon dow command. For multiple values you can separate with comma, use dash or use slash, for example 0,15,30,45 0,6,12,18 1,15,31 * 1-5 * is the same as */15 */6 1,15,31 * 1-5 *.

  • repeat last command with sudo sudo !!
  • copy all files from folder (including hidden) cp folder/. /some-other/ -r Note . after folder name.
  • preserve env variables with sudo sudo -E ruby some_script.rb, but rvmsudo ruby some_script.rb works better
  • ps aux | grep process_name will return always the grep process because it match. But you can use regex so it is not matched ps aux | grep [p]rocess_name. to show full command for process id you can use `ps -fp `. You can kill one by one using `top` and `k`. To find proccess by port it is using you can `lsof -wni tcp:3000`. You can see top 5 proccesses and commands ~~~ ps aux | sort -nrk 3,3 | head -n 5 watch "ps aux | sort -nrk 3,3 | head -n 5" # or with top top -b -n 1 | head -n 12 | tail -n 5 # or better htop ~~~
  • edit long commands in bash set -o vi and than press Esc, and v (visual). Don’t recomment to put in bashrc since it will disable bash shortcuts like: ctrp+p, bind, ….
  • mount sudo fdisk -l sudo blkid sudo vi /etc/fstab add line

    # /ets/fstab
    UUID=428c1c5c-7ef4-480a-aa3b-1b62c3feab98 /mnt/moj_ssd    ext4    defaults  0  2
    

    run sudo chown -R orlovic.orlovic /mnt/moj_ssd/ this is needed only one time.

  • if you need to answer yes and there is no -y parameter, you can use echo -en "\n\n\n" | command . Or you can install yes command which outputs y\n in a infinite loop yes | command
  • last folder in path basename $(pwd)
  • to check if file does not exists you can use

    if [ ! -f tmp/memory_profile.png ]; then
      echo "File not found"
    fi
    
  • run multiple long running commands (like watch tail -f) with ;
  • show commands being executed

    set -x
    command
    set +x
    

Pipe

  • you can pipe output of some command to file c >> a.log, and in other shell tailf a.log but output will be buffered So you need to sudo apt-get install expect-dev and run with unbuffer c >> a.log
  • if you need pipe inside string heroku logs -t | -o app.web.1 than you have to eval $my_string
  • you can add a row to some pipe (echo first line; cat file; echo last line) | some_command or with ps | { echo "header"; grep "something"; }
  • replace a string in pipe with sed. You can create a long running pipe with tailf and echo >>

    tailf my.txt | sed 's/000/111/g'
    
    echo "asd 000 asd 000" >> my.txt
    
  • for redirection standard error to standard output ` 2>&1 ` there is shorthand
    |& version.  
  • if you need to redirect all script output to a file use exec
    #!/bin/bash
    exec >> my_script.log
    exec 2>&1
    echo "This will be saved in a log when you run the script"
    
  • last command exit status echo $? (0 is success)
  • current script pid echo $$
  • last background process id sleep 100 & echo $!
  • jobs -l is a list of processes in current bash session (it is Running if in background with cmd &, or Stopped if Ctrl+z or Terminated when we kill and until it dissapear). Kill first of them with kill %1. If process id is saved in pid file than you can kill with pkill -F tmp/pids/server.pid -9

Parameter expansion

You can find substring

  • call function with $(function_name) and print variable with $variable_name (without brackets). If you need to concatenate with other string you can use curly braces ${variable_name}other_string
  • substring removal

Arrays

linuxjournal

arr=(Dule Orlovic)
${arr[*]}         # Dule Orlovic All of the items in the array
${!arr[*]}        # 0 1 All of the indexes in the array
${#arr[*]}        # 2 Number of items in the array
${#arr[0]}        # 4 Length of item zero

# iterate array in for loog
for var in "${arr[@]}"
do
  echo "${var}"
done

# iterate over sequence
for i in {1..5}; do echo $i; done

Curl

https://gist.github.com/subfuzion/08c5d85437d5d4f00e58

  • same url to some variable
    export u=http://localhost:3000/api/v1/
    curl $u/expenses
    
  • you can add -v --verbose option to see more info or save all communications with url $u --trace-ascii dump.txt

  • see headers
curl $u/expenses -I # or --head fetch the headers only
curl $u/expenses -i # or --include show also the response headers
  • to keep session in curl you can -c write and -b read from cookie file
    curl -c ~/Downloads/cookiefile -b ~/Downloads/cookiefile http://www.google.com
    
  • test the speed on ssh on remote server:
    • download: curl -o /dev/null http://speedtest.qsc.de/1GB.qsc
    • upload: generate large file fallocate -l 1G gentoo_root.img and use scp to test upload link
  • limit the speed to simulate slow connections curl $u limit-rate 100 or using

https://github.com/bcoe/crapify. But this is only for downloading, server renders quickly. Only way to simulate high response time is with sleep 5

  • curl url must be inside '', for example curl http://trk.in.rs?a=2&b=3
  • to get json request use header tag Accept curl -H "Accept: application/json" http://localhost:3001/
  • set authorization header curl $u/expenses -H 'Authorization: Token token="c576f0136149a2e2d9127b3901015545"'
  • user agent curl --user-agent "Mozilla/4.73 [en] (X11; U; Linux 2.2.15 i686)" $u
  • http basic auth curl $u -u username:password or curl $u -u $ADMIN_USERNAME:$ADMIN_PASSWORD'. Another format is inside uri curl http://$username:$password@localhost:3000
  • follow redirection curl $u --location (it works also when performing POST, since it will perfome GET to redirected url).
  • referrer curl $u --referer http://google.com
  • domain name resolution could be using resolve curl http://a.b:3000 --resolve a.b:3000:127.0.0.1 so in rails request.host == 'a.b'
  • POST request is with --data curl $u --data "name=my name" (recent curl will encode post data for you). To see how actual form sends data, save the page localy and change method to GET so when you submit you can see data separated with ? and &
  • patch request on rails is done with --request PATCH option
  • verifyhost

    some sites prevents curl and ruby scripts, if gives forbidden or empty response: https://www.theguardian.com

SSH

You can enable ssh agent forwaring so remote connection can use your keys. When is it enabled there is env | grep SSH_AUTH_SOCK environment variable. You can enable with ssh -A option.

ssh -A vagrant@127.0.0.1 -p 2222 'env | grep SSH_AUTH_SOCK'

Connect using pem or rsa key is ~~ ssh -i pem_file host


`Too many authentication failures for` error is caused by inadvertently offering
multiple ssh keys to the server. You can limit to only one identity

ssh -i some_id_rsa -o -o IdentitiesOnly=yes host


# Rsyslog

Install
[mongo](https://www.digitalocean.com/community/tutorials/how-to-install-mongodb-on-ubuntu-16-04)

sudo apt-key adv –keyserver hkp://keyserver.ubuntu.com:80 –recv EA312927 echo “deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list sudo apt-get update sudo apt-get install -y mongodb-org

cat « HERE_DOC | sudo tee -a /etc/systemd/system/mongodb.service [Unit] Description=High-performance, schema-free document-oriented database After=network.target

[Service] User=mongodb ExecStart=/usr/bin/mongod –quiet –config /etc/mongod.conf

[Install] WantedBy=multi-user.target HERE_DOC


sudo systemctl start mongodb sudo systemctl status mongodb sudo systemctl enable mongodb


https://datatables.net/development/server-side/php_mongodb

# MACBook macOS

xprop for macos can be installed using: `sudo port install xdotool`
Find and xargs can be used also:

find . -name .DS_Store -print0 | xargs -0 git rm -f –ignore-unmatch


[xsel](https://linux.die.net/man/1/xsel) equivalent is
[pbcopy](https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man1/pbcopy.1.html)
so I use [is_mac_os]() to determine os.

I like mac Cmd key so to use that on ubuntu, I need to remap keys. One solution
is to switch ctrl and alt keys, but problem is that Alt+Tab, Alt+1, Ctrl+C,
Crtl+D, should stay as it was.

# Tips

* history
  * bash update history only on exit, but you can manually write with `history
      -a` (do not do this for every command since multiple terminal will be
      [interlieved](http://mywiki.wooledge.org/BashFAQ/088))
  * to add command to history without executing it `history -s pwd`. Note that
    this command if executing as param to bash will open new `history` file so
    we can't have that command in history. Running history when you get console
    is not the same when you pass history as param to bash command. For example
    the following command will show pwd and empty history (if you repeat
    `history` it will give you results) `gnome-terminal -x bash -lc "cd
    ~/Downloads;bash --rcfile <(echo 'pwd;history;watch ls')"`. Solution is to
    add command before invoking bash like [here](
* `cat /etc/issue` and `arch`

$ cat /etc/issue # Ubuntu 16.04.1 LTS \n \l

arch # x86_64


* move file to another file appending some suffix without writting filename
twice

mv config/database.yml{,.example}


* get second string: `echo 'first second' | awk '{print $2}'`
* in bash [3 kinds of variable
substitution](http://www.linuxjournal.com/article/8919)
  * pattern matching: deletes match `#` shortest from left, `##` longest from
  left, `%` shortest from right, `%%` longest from right
    * `NAME=${MYVAR%:*}`  retain the part before the colon `:`
    * `NAME=${NAME##*/}`  retain the part after the last slash `/`
  * substitution `${foo:-bar}`
* [string manipulation](http://tldp.org/LDP/abs/html/string-manipulation.html)

* `wget trk.in.rs --recursive` will download all files from the site.
* sometimes when using multiprocess puma as rails server, I do not see what I'm
typing in
[bash](https://askubuntu.com/questions/171449/shell-does-not-show-typed-in-commands-reset-works-but-what-happened)
so I need to run `reset` command
* to select vim as default editor for root, run `update-alternatives --config
editor` and choose vim.basic
* files need bit `r` to be able to read, `w` to write and `x` to list, search or
source included files.
You can see permissions for particular directory with `ls -ld`. To set for
example `chmod 644` is the same as `chmod u=rw,g=r,o=r`. To be able to source
file, parent folder needs to have `x` flag enabled.
* to detect if it is linux or MacOS you can use

if [ “$(uname -s)” = “Darwin” ]; then echo “==> Bootstrapping Homebrew OSX environment” fi

* screen command
  * `screen -r` to reattach to window (which you exited with Ctrl+A+D). Ctrl+A+]
  to go to copy mode.
* cheatsheet https://devhints.io/bash
* `pbcopy` is only on mac
* to prevent `ctrl-d` to exit terminal bash you can set `IGNOREEOF=3` so shell
  only exists after the 3 consecutive Ctrl-d.
* https://github.com/wting/autojump use j or autojump instead of cd change
  directory
* split long text files with `split -C 50m input output --numeric-suffixes`

* in scripts use

set -e # Any commands which fail will cause the shell script to exit immediately set -x # show command being executed


You can read user input

read -p “enter fullname: “ fullname read -p “Continue? (Y/N): “ confirm && [[ $confirm == [yY] || $confirm == [yY][eE][sS] ]] || exit 1


* xargs is used to convert stdin standard input to arguments for a commands.
  can reference arguments with `-I '{}'`, limit number of lines `-L 2` before
  it is executed (usefull for long running proccess to trigger commands). You
  can not call custom functions since commands are executed in plain bash, but
  you can use `sh` and params `{}` so there you can inline your script

echo 123 | xargs -0 -I ‘{}’ sh -c ‘echo {}’


* read from stdin works fine http://tldp.org/LDP/abs/html/internal.html#READREF
  but not with pipe
  <https://stackoverflow.com/questions/2746553/bash-script-read-values-from-stdin-pipe/6779351#6779351>
  For pipe you need to simulate with `{ }`

* add tab suggestions for your script using `complete` command
  https://iridakos.com/tutorials/2018/03/01/bash-programmable-completion-tutorial
* strip new line from command output `xdotool search --classname
  vp_3_class_slash | tr -d '\n'`
* fan working when high temperature

sudo apt install lm-sensors sudo sensors-detect sensors

sudo apt install hardinfo hardinfo ~~~