Linux

Q1: How can I check if a variable is empty in bash?

In bash at least the following command tests if $var is empty:

if [[ -z "$var" ]]

The command man test is your friend.

Presuming bash:

var=""

if [[ -z "$var" ]]; then
 echo "not empty"
 else
 echo "empty"
 fi

Q2: How to assign a shell command output to a variable?

var=$(command-name-here)
var=$(command-name-here arg1)
var=$(/path/to/command)
var=$(/path/to/command arg1 arg2)

OR

var=command-name-here var=command-name-here arg1 var=/path/to/command var=/path/to/command arg1 arg2

Q3: A motion detection or video streaming utility on Linux

https://github.com/Motion-Project
A note on using motion on Raspberry Pi with the camera module (Rasp V3 with camera V2)
When you run motion, you always get a “unable to open video device” error and a grey screen
Solution:

You can access the camera board on /dev/video0 by running the command:

sudo modprobe bcm2835-v4l2

This will have to be run on every boot of the device. Or you can put modprobe bcm2835-v4l2 in /etc/rc.local to make it run on every boot automatically.

Q3: How to turn off font anti-alias in Ubuntu (to further change the default font, install “unity-tweak-tool”.
You can control the antialiasing of fonts in ~/.fonts.conf

<?xml version='1.0'?>
<!DOCTYPE fontconfig SYSTEM 'fonts.dtd'>
<fontconfig>
<match target="font">
<edit mode="assign" name="antialias">
<bool>false</bool>
</edit>
</match>
</fontconfig>

 

Q4: The default font of Sublime Text on Windows) is Consolas and you might change it >preference>setting. You might also install it on Ubuntu and MacOS

Q5: How to create a soft or symbolic link?

I am installing p4v in /opt, but /usr/bin is on my path. Is it possible to create a soft or symbolic link for p4v from /opt to /usr/bin, so I can just type “p4v” since /usr/bin is in my path?

See man ln.

To create a symlink at /usr/bin/bar which references the original file /opt/foo, use:

ln -s /opt/foo /usr/bin/bar

You would need to apply the above command as root (i.e. with sudo).

Q6: How to increase/decrease the icon size on Ubuntu 18 launchpad (apps grid)?

edit /usr/share/gnome-shell/theme/gnome-shell.css (search for #dash. then change the icon size)

After that, restart the gnome shell by hit alt + F2, and type r and hit “enter”.

Q7: How to change cursor size on Ubuntu 18?

Go to “setting” application, click the Universal Access Tab and click Cursor size under the Seeing column.

Q8: How to install and run Cloud9 IDE on your local Ubuntu?

Cloud9’s git repository and instructions have changed since the other answer was posted. See https://github.com/c9/core/ for more information. The following instructions seem to work for me on a vanilla Ubuntu 14.04.

  • Install Git if you haven’t already:
    sudo apt-get update && apt-get install build-essential
    sudo apt-get install git
  • Install node.js if you don’t already have a recent version installed:
    # Install node.js
    wget -O  ~/node-v0.10.33-linux-x64.tar.gz http://nodejs.org/dist/v0.10.33/node-v0.10.33-linux-x64.tar.gz
    tar -zxf ~/node-v0.10.33-linux-x64.tar.gz
    rm       ~/node-v0.10.33-linux-x64.tar.gz
    echo 'export PATH=$PATH:~/node-v0.10.33-linux-x64/bin' >> ~/.bashrc
    source ~/.bashrc
  • Download and setup Cloud9:
    # Setup and start Cloud9 server
    # (You can get a zip file instead of using git)
    git clone https://github.com/c9/core.git c9sdk
    c9sdk/scripts/install-sdk.sh
  • After the server starts successfully, you can stop it with Ctrl-C. Then you can start it with a different workspace:
    node c9sdk/server.js -w ~/my_workspace/
  • Visit http://localhost:8181 to see the Cloud9 IDE in your browser.
  • If you want to access c9 IDE from another computer on the LAN, you should runnode c9sdk/server.js -w ~/my_workspace/ -p 8080 -l 192.168.31.200 -a :

After the SDK has started navigate to http://192.168.31.200:8080 in your browser to load the IDE.

reference: https://cloud9-sdk.readme.io/docs/running-the-sdk

Q9: Startup scripts when using Bash shell

When Bash starts, it executes the commands in a variety of dot files. Though similar to Bash shell script commands, which have execute permission enabled and an interpreter directive like #!/bin/bash, the initialization files used by Bash require neither.

Execution order of startup files

When started as an interactive login shell

Bash reads and executes /etc/profile (if it exists). (Often this file calls /etc/bash.bashrc.)

After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile in that order, and reads and executes the first one that exists and is readable.

When a login shell exits

Bash reads and executes ~/.bash_logout (if it exists).

When started as an interactive shell (but not a login shell)

Bash reads and executes /etc/bash.bashrc and then ~/.bashrc (if it exists). This may be inhibited by using the --norc option. The --rcfile file option forces Bash to read and execute commands from file instead of ~/.bashrc.

Q10: How to use nohup with SSH (running a command even after you log out)

Basically you can do it in either way:

Directly run the command{,s}

ssh user@host "nohup command1 > /dev/null 2>&1 &; nohup command2; command3"

OR

ssh user@host "$(nohup command1 > /dev/null 2>&1 &) && nohup command2 >> /path/to/log 2>&1 &"

NOTE: && requires the first command to return 0 before executing the second

Use Here document

ssh user@host << EOF
nohup command1 > /dev/null 2>&1 &
nohup command2 >> /path/to/command2.log 2>&1 &
......
EOF

The above 3 options should work for you.

Q11: How to create a new user in mysql and create a database and assign it to the new user?

Assuming you have installed mariaDB, and successfully configure it.

First step is to login

sudo mysql -u root -p

Then, go ahead and create the new user

CREATE USER 'new_user_name'@'localhost' IDENTIFIED BY 'password';

Here ‘password’ should be replaced by your chosen password inside single quote.
After that, create the new database

CREATE database new_db_name;

Then, assign the database to the new user

GRANT ALL PRIVILEGES ON new_db_name.* TO 'new_user_name'@'localhost';