Linux:Scripts: Difference between revisions
From Cheatsheet
Jump to navigationJump to search
(→Common) |
(→if) |
||
| Line 21: | Line 21: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== if === | === if === | ||
Testing file | |||
<pre> | <pre> | ||
https://stackoverflow.com/questions/638975/how-do-i-tell-if-a-file-does-not-exist-in-bash | |||
FILE=/home/javier/wopper.txt | |||
[ -b "$FILE" ] = Block special file | |||
[ -c "$FILE" ] = Special character file | |||
[ -d "$FILE" ] = If file exists | [ -d "$FILE" ] = If file exists | ||
[ -f "$FILE" ] = If directory exists | [ -e "$FILE" ] = Check for file existence, regardless of type (node, directory, socket, etc.) | ||
[ -f "$FILE" ] = Check for regular file existence not a directory | |||
[ -G "$FILE" ] = Check if file exists and is owned by effective group ID | |||
[ -G "$FILE" ] set-group-id - True if file exists and is set-group-id | |||
[ -k "$FILE" ] = Sticky bit | |||
[ -L "$FILE" ] = Symbolic link | |||
[ -O "$FILE" ] = True if file exists and is owned by the effective user id | |||
[ -r "$FILE" ] = Check if file is a readable | |||
[ -S "$FILE" ] = Check if file is socket | |||
[ -s "$FILE" ] = Check if file is nonzero size | |||
[ -u "$FILE" ] = Check if file set-user-id bit is set | |||
[ -w "$FILE" ] = Check if file is writable | |||
[ -x "$FILE" ] = Check if file is executable | |||
FOLDER=/home/javier/ | |||
[ -f "$FOLDER" ] = If directory exists | |||
</pre> | </pre> | ||
Revision as of 14:08, 22 September 2023
Common
# Execute a command within all objects in a folder. for i in *; do du -h "$i" ; done
# Use the test functionality [] to compare values and types, using expressions # man test [] if [ -d "$D" ] then else fi
if
Testing file
https://stackoverflow.com/questions/638975/how-do-i-tell-if-a-file-does-not-exist-in-bash FILE=/home/javier/wopper.txt [ -b "$FILE" ] = Block special file [ -c "$FILE" ] = Special character file [ -d "$FILE" ] = If file exists [ -e "$FILE" ] = Check for file existence, regardless of type (node, directory, socket, etc.) [ -f "$FILE" ] = Check for regular file existence not a directory [ -G "$FILE" ] = Check if file exists and is owned by effective group ID [ -G "$FILE" ] set-group-id - True if file exists and is set-group-id [ -k "$FILE" ] = Sticky bit [ -L "$FILE" ] = Symbolic link [ -O "$FILE" ] = True if file exists and is owned by the effective user id [ -r "$FILE" ] = Check if file is a readable [ -S "$FILE" ] = Check if file is socket [ -s "$FILE" ] = Check if file is nonzero size [ -u "$FILE" ] = Check if file set-user-id bit is set [ -w "$FILE" ] = Check if file is writable [ -x "$FILE" ] = Check if file is executable FOLDER=/home/javier/ [ -f "$FOLDER" ] = If directory exists
FILE=~/todo.txt if [ -f "$FILE" ]; then cat "~/todo.txt" else echo "“Je hebt nog geen todo.txt! Maak deze z.s.m. aan.”" fi
SSH
centos-create-user.sh
#!/bin/bash
# Execute as root
# Usage: ./centos-create-user.sh USER
USER="$1"
echo "Creating user ${USER}"
useradd -m ${USER} -G wheel
echo "Creating authorized keys file and settings rights for ${USER} user"
mkdir -p /home/${USER}/.ssh
cat << 'EOF' >> /home/${USER}/.ssh/authorized_keys
# PUT ANY KEYS IN HERE
# KEY-1
# KEY-2
# KEY-3
EOF
chown -R ${USER}:${USER} /home/${USER}/.ssh
chmod 700 /home/${USER}/.ssh
chmod 600 /home/${USER}/.ssh/authorized_keys
# EOF
ubuntu-create-user.sh
#!/bin/bash
# Execute as root
# Usage: ./ubuntu-create-user.sh USER
USER="$1"
echo "Creating user ${USER}"
adduser ${USER} --disabled-password
echo "Adding ${USER} to the sudo group"
usermod -aG sudo ${USER}
echo "Creating authorized keys file and settings rights for ${USER} user"
mkdir -p /home/${USER}/.ssh
cat << 'EOF' >> /home/${USER}/.ssh/authorized_keys
# PUT ANY KEYS IN HERE
# KEY-1
# KEY-2
# KEY-3
EOF
chown -R ${USER}:${USER} /home/${USER}/.ssh
chmod 700 /home/${USER}/.ssh
chmod 600 /home/${USER}/.ssh/authorized_keys
# EOF
Automatic sendmail
#!/bin/bash
#requires: date,sendmail
function fappend {
echo "$2">>$1;
}
YYYYMMDD=`date +%Y%m%d`
# CHANGE THESE
TOEMAIL="ADMINISTRATOR@MYDOMAIN.COM";
FREMAIL="FROMNOREPLY@MYDOMAIN.com";
SUBJECT="E-mail Subject";
MSGBODY=$(Command);
# DON'T CHANGE ANYTHING BELOW
TMP=`mktemp`
rm -rf $TMP;
fappend $TMP "From: $FREMAIL";
fappend $TMP "To: $TOEMAIL";
fappend $TMP "Reply-To: $FREMAIL";
fappend $TMP "Subject: $SUBJECT";
fappend $TMP "";
fappend $TMP "$MSGBODY";
fappend $TMP "";
fappend $TMP "";
cat $TMP|/usr/sbin/sendmail -t;
rm $TMP;