m Ubuntu Bash

Key Points

  1. Ubuntu good for many purposes
  2. IBM focuses on Red Hat
  3. Alpine now used for Hyperledger


References

Reference_description_with_linked_URLs__________________________Notes__________________________________________________________________








https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.htmlonline Bash man pages
https://linuxhandbook.com/linux-alias-command/Linuxhandbook free version


Articles
file:///C:/Users/Jim%20Mason/Google%20Drive/_docs/howto/linux/linux-cmd-cheatsheet-0.pdf

Linux Command Cheat sheet

file:///C:/Users/Jim%20Mason/Google%20Drive/_docs/howto/linux/linux-cmd-cheatsheet-0.pdf



https://www.computerhope.com/unix/sftp.htmsftp

https://www.ssh.com/ssh/putty/windows/

https://www.ssh.com/ssh/putty/linux/

putty ssh


https://docs.gitlab.com/ee/ssh/

gitlab-ssh-keys-docs.gitlab.com-GitLab and SSH keys.pdf

SSH key generation for remote access to Github  Gitlab etc



Rsynch



IBM i remote access tools



Key Concepts


Remote access using SSH or SFTP

for Putty see m Linux Remote Access Tools


Bash

most Linux platforms have a Bash shell

~/bashrc 



Bash commands 


may have to download man support if not available 

https://www.geeksforgeeks.org/alias-command-in-linux-with-examples/

find commands 


find  -E -H  -X   -f ./_gd/work  iregex ".*log" 2>/dev/null  -exec ls - {} \;  > ./temp/_f_log1.txt

find from a directory, search for files ending in 'log', redirect errors to null device, execute list to an outfile


Google Drive on find command

find  /Volumes/'Google Drive'/'My Drive'/_writing  -iregex '.*gdoc.*'  2>/dev/null  -exec ls -l {} \;


alias commands 

https://www.geeksforgeeks.org/alias-command-in-linux-with-examples/

alias -p. // list all aliases

alias. dothis='my cmd string eg ls -Al ./"my folder" ' 

unalias x



trap commands - similar to monmsg


https://linuxhandbook.com/bash-trap-command/?ref=lhb-linux-digest-newsletter

bash script to send a signal when user interrupts a sleep function in the script

#!/usr/bin/env bash

function sig_int() {
    echo "The sleep was disturbed by Jarasandha"
}

trap sig_int SIGINT

sleep 10m


It is listening for the SIGINT signal and upon receiving it, it will call the sig_int function.

Let's try this!

$ ./trap-script.sh
^CThe sleep was disturbed by Jarasandha

As soon as I pressed the Ctrl + C key (as evident by ^C), the echo statement inside the body of the sig_int function was called.


Sending other signals

SIGINT is not the only signal that you might want to monitor for. Therefore, you may want to verify whether your script is correctly listening for intended signals. In that case, you can send a signal externally using the kill command.

Following is the syntax for this particular use:

kill -s SIGNAL PID

As it might be obvious now, use the -s option to specify which signal you intend to send. Then, specify the PID of the program to which you want to send the signal.

I will make a slight modification to the way I execute this script... I will run it in the background so I can use the same shell to run the kill command to send the signal.

$ ./trap-script.sh &
[1] 28550

$ kill -s SIGINT 28551

$ The sleep was disturbed by Jarasandha

[1]  + exit 130   ./trap-script.sh

Look closely. The PID of the script that was put in the background is 28550 but I am sending SIGINT to the shell script's PID + 1 PID (28551). The reason is that the active process is the sleep command which is running inside our bash script ;)

You are actually monitoring the signals for the commands running inside the script. Not the script itself.



Creating a system service with systemd

https://linuxhandbook.com/create-systemd-services/?ref=lhb-linux-digest-newsletter

a service is a "background process" that is started or stopped based on certain circumstances. You are not required to manually start and/or stop it. A 'systemd service file' is a file that is written in a format such that systemd is able to parse and understand it, and later on do what you told it to do.

The systemd service file has three important and necessary sections. They are [Unit], [Service] and [Install] sections. The systemd service file's extension is .service and we use the pound/hash symbol (#) for single line comments.

Below is an example of a systemd service file. Please note that this is NOT an actual systemd service file. I have modified it so that it helps you understand.

[Unit]
Description=Apache web server
After=network.target
Before=nextcloud-web.service

[Service]
ExecStart=/usr/local/apache2/bin/httpd -D FOREGROUND -k start
ExecReload=/usr/local/apache2/bin/httpd -k graceful
Type=notify
Restart=always


[Install]
WantedBy=default.target
RequiredBy=network.target

This is the most basic structure of a systemd service file. Let us understand what each of these words mean.


Example - Systemd service for root user

I have written a script, that I want to run at the time of system boot, as the root user. The script is called sys-update.sh, and it's absolute file path is /root/.scripts/sys-update.sh; Below is the script:

#!/usr/bin/env bash

if [ ${EUID} -ne 0 ]
then
	exit 1 # this is meant to be run as root
fi

apt-get update 1>/dev/null 2>>/root/logs/sys-update.log

Let us first understand what this script does. First, it will check if the user is root or not. If the user is the root user, then the apt-get update command will be executed. Any errors from the output of that command will be appended to the /root/logs/sys-update.log file. And any additional output will be redirected to the /dev/null file.


Usually, it is considered a good practice to put these systemd service files inside the /etc/systemd/system/ directory.

Therefore, I will create a file update-on-boot.service; its full path being /etc/systemd/system/update-on-boot.service. Below are the contents of this service file:

[Unit]
Description=Keeping my sources fresher than Arch Linux
After=multi-user.target

[Service]
ExecStart=/usr/bin/bash /root/.scripts/sys-update.sh
Type=simple

[Install]
WantedBy=multi-user.target

This is a very simple systemd service file. All it does is use the bash interpreter to interpret the /root/.script/sys-update.sh script and execute it.

Enabling the service

Now that the systemd service file is ready and placed under the /etc/systemd/system/ directory, let us look at how to enable it.

To tell systemd to read our service file, we need to issue the following command:

sudo systemctl daemon-reload

Doing so will make systemd aware of our newly created systemd service file.

Now, we can enable our systemd service. 

In our case, the service is named update-on-boot.service, so I will run the following command:
sudo systemctl enable update-on-boot.service

Below is the output of checking the status of the service:

$ sudo systemctl is-enabled update-on-boot.service
enabled


Systemctl Status Command to Check Service Status

https://linuxhandbook.com/systemctl-check-service-status/?ref=lhb-linux-digest-newsletter

systemctl status <service-name>

you can always list systemd services and get the name.

Status

  • active (running): Service is actively running in the background.
  • active (exited): Indicates the service was meant to be executed one time or periodically. So the service did its job and then exit upon completion.
  • active (waiting): It indicates the service is running but it is waiting to be triggered by some condition like a specific event.
  • inactive: Service is not currently running.
  • enabled: Service will be enabled at system boot time.
  • disabled: Service is disabled and won't be started at system boot.
  • static: It means the specific service can't be managed using systemd (or the systemctl command) and you'd need to have another init service or manage it manually.
  • masked: This means the service is masked and can't be stated directly using the systemctl command. This can be helpful when you want to prevent accidental starting of service.
  • alias: It indicates the service name is an alias and the service is a symlink pointing to another unit file.
  • linked: It indicates that the service or the unit file is symbolically linked to another unit file.


sudo systemctl start | stop <service_name>
sudo systemctl enable | disable <service_name>


Windows Gitbash shell

Windows 10 offers download of Gitbash

Gitbash does not include man pages or gzip

Gitbash does include git

to switch directories on Windows >>     cd /D/mydir


Windows provides basic help in the shell

help lists all bash commands

help git lists the commands and options for git




Putty SSH


copy and paste text in putty

To copy from Windows and paste into PuTTY, highlight the text in Windows, press "Ctrl-C," select the PuTTY window, and press the right mouse button to paste.

To copy from PuTTy and paste into Windows, highlight the information in PuTTY and press "Ctrl-V" in the Windows application to paste it.



sftp - secure ftp 

https://www.computerhope.com/unix/sftp.htm


login as a user to a remote host sending password

psftp homenet_dmx_internal@sftp.dmx.io -pw 7Au6XjArGV5w3JE1BqGkrO2EAtedFI


psftp> help

! run a local command

bye finish your SFTP session

cd change your remote working directory

chmod change file permissions and modes

close finish your SFTP session but do not quit PSFTP

del delete files on the remote server

dir list remote files

exit finish your SFTP session

get download a file from the server to your local machine

help give help

lcd change local working directory

lpwd print local working directory

ls list remote files

mget download multiple files at once

mkdir create directories on the remote server

mput upload multiple files at once

mv move or rename file(s) on the remote server

open connect to a host

put upload a file from your local machine to the server

pwd print your remote working directory

quit finish your SFTP session

reget continue downloading files

ren move or rename file(s) on the remote server

reput continue uploading files

rm delete files on the remote server

rmdir remove directories on the remote server



Potential Value Opportunities



Potential Challenges



Candidate Solutions



Step-by-step guide for Example



sample code block

sample code block
 



Recommended Next Steps