unable to execute shell script as a user - linux

I've created one script for environment setup. I need Oracle to be installed as part of the script. below is the script i've written (pardon for errors, i'm new). The problem is, when i executed it, they are running fine individually, but collectively, the script is exiting and failing after "UNZIP" command. Can someone please help?
sudo -i -u oracle mkdir /home/oracle/dump
sudo -i -u oracle cd /home/oracle/dump
JAR1="p13390677_112040_Linux-x86-64_1of7.zip"
JAR2="p13390677_112040_Linux-x86-64_2of7.zip"
folder="/home/oracle/database"
if test -s "$JAR1"
then
echo "$JAR1 exists"
else
sudo -i -u oracle
wget XXXp://XXXXXX/XXXXXX/download/database/11.2.0.4/p13390677_112040_Linux-x86-64_1of7.zip
fi
if test -s "$JAR2"
then
echo "$JAR2 exists"
else
sudo -i -u oracle
wget XXXp://XXXXXXX/software/download/database/11.2.0.4/p13390677_112040_Linux-x86-64_2of7.zip
fi
if test -s "$folder"
then
echo "$folder exists"
else
unzip /home/oracle/p13390677_112040_Linux-x86-64_1of7.zip
unzip /home/oracle/p13390677_112040_Linux-x86-64_2of7.zip
fi
sudo -i -u oracle ORACLE_HOSTNAME=`hostname`
sudo -i -u oracle ./runInstaller $ORACLE_HOSTNAME -silent -responseFile /home/oracle/db.rsp
cd /opt/db/oraInventory
./orainstRoot.sh
cd /opt/db/oracle/11.2.0
./root.sh

Related

Jira doesn't seem to be installing on my EC2 instance at all: Failed to start jira.service: Unit jira.service not found

I'm trying to run a user-data script to install Jira on my RHEL 8 EC2 instance. When I SSH into my instance and run ps -ef | grep jira which I found on their website it returns this in my terminal
ec2-user 5256 5199 0 20:39 pts/0 00:00:00 grep --color=auto jira
I also try running this command in my terminal to start Jira sudo systemctl start jira and this is the error I get Failed to start jira.service: Unit jira.service not found
I'm not too sure what's causing Jira not to be installed or why I can't start it manually when I SSH into my instance- here's my user-data script that I'm using to install jira
#!/usr/bin/env bash
set \
-o nounset \
-o pipefail \
-o errexit
echo "===== Downloading Jira ====="
cd ~
wget https://www.atlassian.com/software/jira/downloads/binary/atlassian-jira-software-8.22.4-x64.bin
chmod +x atlassian-jira-software-8.22.4-x64.bin
wget -O ~/jira.bin ${jira_dl_url}
cat <<\EOF >> ~/jira.varfile
app.confHome=/var/atlassian/application-data/jira
app.install.service$Boolean=false
portChoice=custom
httpPort$Long=8080
rmiPort$Long=8443
launch.application$Boolean=false
sys.adminRights$Boolean=true
sys.confirmedUpdateInstallationString=false
sys.installationDir=/opt/atlassian/jira
sys.languageId=en
EOF
echo "done"
echo "===== Modify Jira Permissions ====="
chmod +x ~/jira.bin
echo "===== Start Jira Install ====="
sudo ./atlassian-jira-software-8.22.4-x64.bin -q
#sudo ~/jira.bin -q -varfile ~/jira.varfile
#sudo /opt/atlassian/jira/bin/startup.sh
echo "===== Stopping Jira Service ====="
sudo systemctl stop jira
echo "====== Change Ownership to Jira user ========="
sudo chown -R jira:jira /opt/atlassian/jira
sudo chown -R jira:jira /var/atlassian/application-data/jira
echo "===== Cleaning up Jira Files ====="
rm -f ~/jira.bin
rm -f ~/jira.varfile
chmod 750 /opt/atlassian/jira/logs/

How to check if user has sudo privileges inside the bash script?

I would like to check if the user has sudo privileges. This is an approximate example of what I am trying to do. I am trying to get this to work across the following os: centos, ubuntu, arch.
if userIsSudo; then
chsh -s $(which zsh)
fi
Try with this:
$ sudo -v &> /dev/null && echo "Sudoer" || echo "Not sudoer"
Also, IDK how secure will be searching for his membership in the sudo group, i.e:
$ groups "$(id -un)" \
| grep -q ' sudo ' \
&& echo In sudo group \
|| echo Not in sudo group
Or:
$ getent group sudo \
| grep -qE "(:|,)$(id -un)(,|$)" \
&& echo in sudo group \
|| echo not in sudo group
sudo -l will display the commands that the user can run with sudo privileges. If there are no commands that can be run, sudo -l will return an error code and so you could try:
sudo -l && chsh -s $(which zsh)
Usually when you run an script you want to know if end it well or you got an error or what kind of error you got if there was any.
This is a more elaborated snippet, sudoer-script.sh:
## Define error code
E_NOTROOT=87 # Non-root exit error.
## check if is sudoer
if ! $(sudo -l &> /dev/null); then
echo 'Error: root privileges are needed to run this script'
exit $E_NOTROOT
fi
## do something else you
## means it was successfully executed
exit 0
Now you can reuse your script, pipe it or concatenate with other commands
sudoer-script.sh && ls
## in a script
if $(sudoer-script.sh); then
echo 'success'
fi
## capture error
stderr=$(./sudoer-script.sh 2>&1 >/dev/null)
echo $stderr
As a function:
is_sudoer() {
## Define error code
E_NOTROOT=87 # Non-root exit error.
## check if is sudoer
if ! $(sudo -l &> /dev/null); then
echo 'Error: root privileges are needed to run this script'
return $E_NOTROOT
fi
return 0
}
if is_sudoer; then
echo "Sudoer"
else
echo "Not sudoer"
fi

SSH Bash script issues with if statement

I'm trying to learn how to write Bash scripts. I have this script to update my servers through ssh. I'm trying to add a check and a conditional to determine if the OS uses Yum or Apt then it will run the appropriate update commands. The if else statement seems to be wrong but I'm not sure how to correct this.
Here is the script:
#!/bin/bash
USERNAME="root"
HOSTS="host1 host2 host3"
apt_run="apt update && apt -y upgrade"
yum_run="yum check-update && yum -y update"
for HOSTNAME in ${HOSTS} ; do
ssh -l ${USERNAME} ${HOSTNAME}
find_os=$( command -v yum || command -v apt-get ) || echo "Neither
yum nor apt-get found"
if [[ $find_os='yum' ]]
then
"${yum_run}"
else
"${apt_run}"
fi
done
Here is my script for my virtual machines.
#!/bin/bash
hosts=(
leap151 kali ubuntu omv
)
for hostname in "${hosts[#]}"; do
ssh -t root#"$hostname" << 'EOF'
if type -P zypper >/dev/null; then
command zypper ref && command zypper up
elif type -P apt-get >/dev/null; then
command apt-get update && command apt-get upgrade
else
echo 'Neither zypper nor apt found!' >&2
exit 127
fi
EOF
done
Use an array for the host. Since you're using bash the builtin type is fine just for searching the executable within your PATH. See help type for more info. Use the -t option in ssh also use a heredoc just what I have/did. The exit 127 is what the shell would exit if there are no executable see man 1p exit.

Run sudo command in shell script

I wrote a shell script like this
#!/bin/bash
sudo mkdir /var/www/html/test
sudo cp ./index.html /var/www/html/test/index.html
echo "Hi" > /var/www/html/test/index.html
if I runt this with sudo it works well.
$ sudo ./script.sh
but I don't want to run with sudo. because echo doesn't need root privilege. In other hand if I run this without sudo like this:
$ ./script.sh
for the first command (mkdir) it asks me for root password and second command doesn't run and give me a permission denied error.
How can I handle this situation?
Based on your setup, e.g. in ubuntu if I run sudo 2 times, the second time I don't have to give the password. So it is possible that the second sudo DID run, without asking for password again.
You can clarify, try this:
sudo echo a
sudo echo b
It is most likely, as Kip K commented, the error originates from the echo "Hi"... since the normal user has no permission to write /var/www/html/test/index.html .
Kinda overkill, but you can give constant feedback like this:
sudo bash -c 'echo mkdir; mkdir /var/www/html/test'
sudo bash -c 'echo cp; cp ./index.html /var/www/html/test/index.html'
Try chown for directory test:
#!/bin/bash
sudo bash -c 'mkdir /var/www/html/test && chown -R USER /var/www/html/test'
cp ./index.html /var/www/html/test/index.html
echo "Hi" > /var/www/html/test/index.html
or ... chmod o+w /var/www/html/test

Run command as root within shell script

I'm working on a script that will shred a usb drive and install Kali linux with encrypted persistent data.
#! /bin/bash
cd ~/Documents/Other/ISOs/Kali
echo "/dev/sdx x=?"
read x
echo "how many passes to wipe? 1 will be sufficient."
read n
echo "sd$x will be wiped $n times."
read -p "do you want to continue? [y/N] " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
exit 1
fi
echo "Your role in the installation process is not over. You will be prompted to type YES and a passphrase."
sudo shred -vz --iterations=$n /dev/sd$x
echo "Wiped. Installing Kali"
sudo dd if=kali-linux-2.0-amd64.iso of=/dev/sd$x bs=512k
echo "Installed. Making persistence."
y=3
sudo parted /dev/sd$x mkpart primary 3.5GiB 100%
x=$x$y
sudo cryptsetup --verbose --verify-passphrase luksFormat /dev/sd$x
sudo cryptsetup luksOpen /dev/sd$x my_usb
sudo mkfs.ext3 -L persistence /dev/mapper/my_usb
sudo e2label /dev/mapper/my_usb persistence
sudo mkdir -p /mnt/my_usb
sudo mount /dev/mapper/my_usb /mnt/my_usb
sudo -i
echo "/ union" > /mnt/my_usb/persistence.conf
umount /dev/mapper/my_usb
cryptsetup luksClose /dev/mapper/my_usb
echo "Persistence complete. Installation complete."
It works nearly perfectly. These commands individually entered into the terminal will create the desired effect, but the problem comes in at line 37:
sudo echo "/ union" > /mnt/my_usb/persistence.conf
That command won't work unless I'm logged in as root user. To solve this I tried adding the sudo -i command before, but once I do that all of the following commands are skipped.
It's okay if the solution suggested requires me to type in the password. I don't want the password stored in the script, that's just wreckless.
Side note, I didn't make a generic form for this question because I want other people to be able use this if they like it.
The problem is that the echo runs with root privilege but the redirection happens in the original shell as the non-root user. Instead, try running an explicit sh under sudo and do the redirection in there
sudo /bin/sh -c 'echo "/ union" > /mnt/my_usb/persistence.conf'
The problem is that when you type in the following command:
sudo echo "/ union" > /mnt/my_usb/persistence.conf
Only the "echo" will be run as root through sudo, but the redirection to the file using > will still be executed as the "normal" user, because it is not a command but something performed directly by the shell.
My usual solution is to use teeso that it runs as a command and not as a shell built-in operation, like this:
echo "/ union" | sudo tee /mnt/my_usb/persistence.conf >/dev/null
Now the tee command will be run as root through sudo and will be allowed to write to the file. >/dev/null is just added to keep the output of the script clean. If you ever want to append instead of overwrite (e.g. you would be using >>normally), then use tee -a.

Resources