2019年3月27日 星期三

Bash Scripting: How to Output and Format Text on Linux Shell

Source: https://stackoverflow.com/questions/378829/convert-decimal-to-hexadecimal-in-unix-shell-script

Convert decimal to hexadecimal in UNIX shell script

printf "%x\n" 34
22


Source: https://vitux.com/how-to-output-text-on-linux-shell/

Bash Scripting: How to Output and Format Text on Linux Shell


Bash scripting is quite popular is the easiest scripting language. Like any programming or scripting language, you come across printing text on the terminal. This can happen in numerous scenarios such as when you want to output the contents of a file or check the value of a variable. Programmers also debug their applications by printing the values of their variables on the console. Therefore, before we delve into bash scripting which will be another tutorial, let’s look at the different ways in which we could output text in the terminal.
Echo is the most important command that you need to know in order to output text on the terminal. As the name itself suggests, echo displays number or string on standard output in the terminal. It also has a number of options available as shown in the table below.
OptionsDefinition
-nDo not print the trailing newline
-EDisable interpretation of back-slash escaped characters
-eEnable interpretation of backslash escapes
\aAlert
\bBackspace
\cSuppress trailing newline
\eEscape
\fForm feed
\\backslash
\nNew line
\rCarriage return
\tHorizontal tab
\vVertical tab
According to the Linux documentation, the following is the syntax for echo command.
echo [option(s)][string(s)]
Now, we shall see the different ways in which we can output the text on the terminal.

Send Text to Standard Output


To output any string or number or text on the terminal, type the following command and press enter.
echo "Hello World"
The following output will be shown on the terminal
Send text to stdout with echo command

Print a Variable


Let’s declare a variable and prints its value on the terminal. Suppose x is a variable which we have initialized at 100.
x=100
Now, we will output the value of the variable on the terminal.
echo x
100 will be printed on the terminal. Likewise, you can also store a string in a variable and output it on the terminal.
Print content of a variable
Try it out and let us know if it was easy for you.

Remove Space between Words


This is one of my favorite option of echo as it removes all the space between different words in the sentences and jumbles them up together. In this feature, we will be using two of the options as mentioned in Table 1.
echo -e "Hello \bmy \bname \bis \bjohn \bDoe"
As you can see from the above example, we are enabling the interpretation of backslash escapes as well as adding backspace. The following output was shown.
Remove space between words with backspace

Output Word in New Line


This option of echo comes in really handy when you are working bash scripting. Mostly you need to move to the next line once you are done. Therefore, this is the best option to use for that.
echo -e "Hello \nmy \nname \nis \nJohn \nDoe"
The output will display each word in a separate line as shown in the screenshot below.
Add newlines to the text output

Output Text with Sound


This is a simple option of outputting text with bell or alert. To do this, type the following command.
echo -e "Hello \amy name is John Doe"
Make sure that your system’s volume is high enough for you to hear the tiny bell that sounds when the text is outputted on the terminal.

Remove Trailing New Line


Another option of the echo is to remove the trailing newline so that everything outputs on the same line. For this, we use “\c” option as shown in the figure below.
echo -e "Hello my name \cis John Doe"
The following output is shown
Remove trailing newline

Add a Carriage Return to the output


To add a specific carriage return in your output, we have “\r” option for this.
echo -e "Hello my name \ris John Doe"
The following output is shown to you on the terminal.
Add a Carriage Return to the output

Use Tabs in Output


While printing output on the terminal, you can add horizontal and vertical tabs as well. These come in handy for cleaner outputs. To add horizontal tabs, you have to add “\t” and for vertical tabs, add “\v”. We will be doing a sample for each of these and then a combined one.
echo -e "Hello my name \tis John Doe"
The output for this command will be shown as follows
Use tabs to format the text output
echo -e "Hello my name \vis John Doe"
The output for this command will be shown as follows
Use \v in bash output
Now we will combine this example for a set of sentences we have.
echo -e "Hello my name \vis John Doe. Hello! My name is \tJane Doe"
The following will be printed on the terminal.
Advanced formatting example
Well, that’s all the options that can be used for printing text on a terminal. This is an important feature to learn as it will help you further when you start working on bash scripting. Make sure that you implement each of the options and practice hard. Let us know if this tutorial helped you solve a problem.

MS Shell Script

'type' = 'cat' in Linux

'find' and 'findstr' = 'grep' in Linux

'NUL' = '$null' in PowerShell (PS) = '/dev/null' in Linux


2019年3月10日 星期日

Enable shared folder for VirtualBox VM

Source https://blog.gtwang.org/tips/virtualbox-shared-folder/

STEP 1
首先設定 Host OS 中 VirtualBox 的分享資料夾,從「Device」選單中開啟「Shared Folders」設定視窗。
vb1
VirtualBox 的分享資料夾功能選單
點選右方的「Add Shared Folder」圖示。

STEP 2
設定要分享的資料夾路徑與名稱,另外把「Auto-mount」與「Make Permanent」兩個選項也勾起來,
vb2
新增 VirtualBox 的分享資料夾
然後按下「OK」。
vb2-1
這樣在 Host OS 中 VirtualBox 這邊的設定就完成了,接下來是 Guest OS 中的設定。
STEP 3
在 Linux 中(在 Guest OS 中)修改 /etc/group,將所有需要用到分享資料夾的帳號加入 vboxsf 群組。
首先找尋「vboxsf」這個群組,正常來說應該會像這樣:
vboxsf:x:125:
接著把所有需要用到分享資料夾的帳號加進去,例如將 seal 這個帳號加入 vboxsf 群組:
vboxsf:x:125:seal
也就是把帳號名稱加在最後一個冒號之後,如果要加入多個帳號,就用豆點分隔。
STEP 4
登出虛擬機器的 Linux,重新登入。

2019年3月6日 星期三

How to get the application exit code ?

Source https://stackoverflow.com/questions/334879/how-do-i-get-the-application-exit-code-from-a-windows-command-line

BASH
echo $?

Windows
echo Exit Code is %errorlevel%
@echo off
my_nify_exe.exe
if errorlevel 1 (
   echo Failure Reason Given is %errorlevel%
   exit /b %errorlevel%
)

How to disable kernel logs from printing on the console?

Source https://stackoverflow.com/questions/334879/how-do-i-get-the-application-exit-code-from-a-windows-command-line

sudo dmesg -n 1

Access memory-mapped registers in Linux kernel space.

Source https://stackoverflow.com/questions/16935041/how-to-write-register-from-linux-kernel-module-cpu-arm

void __iomem *io = ioremap(REGBASE, SZ_4K);
writel(0x00000002, io + 0x0004);

How to split a shell command over multiple lines (when using an IF statement) ?

Source https://stackoverflow.com/questions/18599711/how-can-i-split-a-shell-command-over-multiple-lines-when-using-an-if-statement

$ cat test.sh
if ! fab --fabfile=.deploy/fabfile.py \
   --forward-agent \
   --disable-known-hosts deploy:$target; then
     echo failed
else
     echo succeeded
fi

$ alias fab=true; . ./test.sh
succeeded
$ alias fab=false; . ./test.sh
failed

“error: branch '.*' not found when removing a branch is attempted.

find .git//refs -name -delete

In case of it is repository hosted by repo,
find .repo//refs -name -delete