-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv_setup.sh
executable file
·74 lines (59 loc) · 2.4 KB
/
env_setup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/bash
# Check if the current user is root
if [ "$(id -u)" != "0" ]; then
echo -e "This script must be run as root. \nPlease run again with 'sudo $0'"
exit 1
fi
# Define the source files for symlinks
src_dit_logger="/home/ditrobotics/DIT-Scripts/DIT-Logger"
src_login_launch="/home/ditrobotics/DIT-Scripts/Login-Launch"
# The current user's username
current_user="ditrobotics"
src_DIT="/var/log/DIT"
mkdir $src_DIT
touch "$src_DIT/$HOSTNAME.log"
chgrp sudo "$src_DIT/$HOSTNAME.log"
chgrp sudo $src_DIT
chmod 750 $src_DIT
chmod 660 "$src_DIT/$HOSTNAME.log"
# Loop through each user in the sudo group
for user in $(grep '^sudo:' /etc/group | cut -d: -f4 | tr ',' ' '); do
# Exclude the current user
if [[ "$user" != "$current_user" ]]; then
# Get the home directory of the user
user_home=$(getent passwd "$user" | cut -d: -f6)
# Create a 'Scripts' directory in the user's home directory
mkdir -p "$user_home/Scripts"
# Function to create symlink if the target does not exist
create_symlink() {
local src=$1
local dest=$2
local link_name=$(basename "$src")
if [[ ! -e "$dest/$link_name" ]]; then
ln "$src" "$dest"
else
echo "Symlink $dest/$link_name already exists, skipping."
fi
}
# Create symlinks for DIT-Logger and Login-Launch in the user's 'Scripts' directory
create_symlink "$src_dit_logger" "$user_home/Scripts"
create_symlink "$src_login_launch" "$user_home/Scripts"
# Change the owner of the Scripts directory to the user
chown "$user":"$user" "$user_home/Scripts"
# Create user log files
touch "/var/log/DIT/last_login_$user"
chown "$user":"$user" "/var/log/DIT/last_login_$user"
# Create bash aliases
if [ ! -f "$user_home/.bash_aliases" ]; then
echo "# Alias for DIT-Scripts" >> "$user_home/.bash_aliases"
echo "~/Scripts/Login-Launch" >> "$user_home/.bash_aliases"
echo "alias DIT-Logger=$user_home/Scripts/DIT-Logger" >> "$user_home/.bash_aliases"
else
echo "File already exists: .bash_aliases"
fi
fi
done
# --- Put your Non-root command below ---
# Copy .bash_aliases to main user (ditrobotics)
su - $current_user -c 'cp $HOME/DIT-Scripts/.bash_aliases $HOME/.bash_aliases'
# --- End of Non-root command ---