Read a File Line By Line in Bash
#!/bin/bash input="/path/to/txt/file" while IFS= read -r line do echo "$line" done < "$input"
References
https://www.cyberciti.biz/faq/unix-howto-read-line-by-line-from-file/
#!/bin/bash input="/path/to/txt/file" while IFS= read -r line do echo "$line" done < "$input"
References
https://www.cyberciti.biz/faq/unix-howto-read-line-by-line-from-file/
sudo dhclient -r
sudo dhclient
renew or release an IP in Linux for eth0
sudo dhclient -r eth0 sudo dhclient eth0
References
https://www.cyberciti.biz/faq/howto-linux-renew-dhcp-client-ip-address/
#!/usr/bin/expect spawn ssh [email protected] expect "assword:" send "mypassword\r" interact
References
https://stackoverflow.com/questions/16928004/how-to-enter-ssh-password-using-bash
To keep an SSH connection alive, you can modify your SSH configuration to send periodic keepalive messages. Here are a few methods to achieve this:
/etc/ssh/ssh_config
or ~/.ssh/config
):
nano ~/.ssh/config
Host * ServerAliveInterval 60 ServerAliveCountMax 3
ServerAliveInterval
specifies the interval in seconds between keepalive messages.ServerAliveCountMax
specifies the number of keepalive messages that can be sent without receiving a response from the server before the connection is terminated./etc/ssh/sshd_config
):
sudo nano /etc/ssh/sshd_config
ClientAliveInterval 60 ClientAliveCountMax 3
ClientAliveInterval
specifies the interval in seconds that the server will wait before sending a null packet to the client to keep the connection alive.ClientAliveCountMax
specifies the number of client alive messages which may be sent without receiving any messages back from the client.sudo systemctl restart sshd
Pass arguments through to another program
#!/bin/bash # print_args.sh echo "You provided the arguments:" "$@" # You could pass all arguments to another program like this # myProgram "$@"
Get the number of arguments passed
#!/bin/bash echo "You provided $# arguments"
Accessing a specific argument by index
#!/bin/bash echo "Arg 0: $0" echo "Arg 1: $1" echo "Arg 2: $2"
Argument 0 is the name of the script being invoked itself.
Iterating through each argument
#!/bin/bash for arg in "$@" do echo "$arg" done
Check arguments for specific value
#!/bin/bash for arg in "$@" do if [ "$arg" == "--help" ] || [ "$arg" == "-h" ] then echo "Help argument detected." fi done
References
https://www.devdungeon.com/content/taking-command-line-arguments-bash
systemctl list-units | grep monitoring
systemctl list-unit-files | grep enabled
References
https://askubuntu.com/questions/795226/how-to-list-all-enabled-services-from-systemctl
echo -n "Some string..."
echo -e "Some string...\c"
printf "a line without trailing linefeed"
printf "a line with trailing linefeed\n"
References
https://stackoverflow.com/questions/11193466/echo-without-newline-in-a-shell-script
systemctl is-active application.service
systemctl is-enabled application.service
systemctl is-active --quiet service
will exit with status zero if service is active, non-zero otherwise, making it ideal for scripts:
systemctl is-active --quiet service && echo Service is running
References
https://www.digitalocean.com/community/tutorials/how-to-use-systemctl-to-manage-systemd-services-and-units
https://unix.stackexchange.com/questions/396630/the-proper-way-to-test-if-a-service-is-running-in-a-script
Check if File Exist
FILE=/etc/resolv.conf if test -f "$FILE"; then echo "$FILE exist" fi
FILE=/etc/resolv.conf if [ -f "$FILE" ]; then echo "$FILE exist" fi
FILE=/etc/resolv.conf if [[ -f "$FILE" ]]; then echo "$FILE exist" fi
Check if Directory Exist
FILE=/etc/docker if [ -d "$FILE" ]; then echo "$FILE is a directory" fi
Check if File does Not Exist
FILE=/etc/docker if [ ! -f "$FILE" ]; then echo "$FILE does not exist" fi
Check if Multiple Files Exist
FILE=/etc/docker if [ -f /etc/resolv.conf -a -f /etc/hosts ]; then echo "$FILE is a directory" fi
FILE=/etc/docker if [ -f /etc/resolv.conf && -f /etc/hosts ]; then echo "$FILE is a directory" fi
File test operators
-f
FILE
– True if the FILE exists and is a regular file (not a directory or device).-G
FILE
– True if the FILE exists and has the same group as the user running the command.-h
FILE
– True if the FILE exists and is a symbolic link.-g
FILE
– True if the FILE exists and has set-group-id (sgid) flag set.-k
FILE
– True if the FILE exists and has a sticky bit flag set.-L
FILE
– True if the FILE exists and is a symbolic link.-O
FILE
– True if the FILE exists and is owned by the user running the command.-p
FILE
– True if the FILE exists and is a pipe.-r
FILE
– True if the FILE exists and is readable.-S
FILE
– True if the FILE exists and is socket.-s
FILE
– True if the FILE exists and has nonzero size.-u
FILE
– True if the exists and set-user-id (suid) flag is set.-w
FILE
– True if the FILE exists and is writable.-x
FILE
– True if the FILE exists and is executable.References
https://linuxize.com/post/bash-check-if-file-exists/
Environment directive
Environment="ONE=one" 'TWO=two two' ExecStart=/bin/echo $ONE $TWO ${TWO}
EnvironmentFile directive
EnvironmentFile similar to Environment directive but reads the environment variables from a text file. The text file should contain new-line-separated variable assignments.This environment file can then be sourced and its variables used
Example file : /run/metadata/coreos
COREOS_DIGITALOCEAN_IPV4_ANCHOR_0=X.X.X.X COREOS_DIGITALOCEAN_IPV4_PRIVATE_0=X.X.X.X COREOS_DIGITALOCEAN_HOSTNAME=test.example.com COREOS_DIGITALOCEAN_IPV4_PUBLIC_0=X.X.X.X COREOS_DIGITALOCEAN_IPV6_PUBLIC_0=X:X:X:X:X:X:X:X
[Unit] Requires=coreos-metadata.service After=coreos-metadata.service [Service] EnvironmentFile=/run/metadata/coreos ExecStart= ExecStart=/usr/bin/etcd2 \ --advertise-client-urls=http://${COREOS_DIGITALOCEAN_IPV4_PUBLIC_0}:2379 \ --initial-advertise-peer-urls=http://${COREOS_DIGITALOCEAN_IPV4_PRIVATE_0}:2380 \ --listen-client-urls=http://0.0.0.0:2379 \ --listen-peer-urls=http://${COREOS_DIGITALOCEAN_IPV4_PRIVATE_0}:2380 \ --initial-cluster=%m=http://${COREOS_DIGITALOCEAN_IPV4_PRIVATE_0}:2380
References
https://stackoverflow.com/questions/37864999/referencing-other-environment-variables-in-systemd
https://coreos.com/os/docs/latest/using-environment-variables-in-systemd-units.html