Link to home
Start Free TrialLog in
Avatar of missymadi
missymadi

asked on

How to get Bash File to Run all Commands in File?

Experts,

        I have the following file in .sh format. In RHEL5 I cannot get past the first step. The script stops on step 2. Not sure why?
##########################################################
#
#Logging: 
#
#
LOG_FILE='/data/CDS_DATA/PS_DBInstall.log'2>&1 >> $LOG_FILE 

#
#
#Steps:
#
#Step 1. Create a profile backup. Replace current profile file with updated file. 2>&1 >> $LOG_FILE 

echo "Backup and copy the environment profile" 2>&1 >> $LOG_FILE 
/bin/cp /etc/profile /etc/profile.bak 2>&1 >> $LOG_FILE 
/bin/cp -f /home/tcdl/Desktop/profile /etc/profile 2>&1 >> $LOG_FILE 
#
#
#Step 2. su to root 2>&1 >> $LOG_FILE *****failing here
sudo su -               
#
#
#Step 3. Create a directory 2>&1 >> $LOG_FILE 
echo "Make /data/CDS_DATA directory" 2>&1 >> $LOG_FILE 
mkdir /data/CDS_DATA 2>&1 >> $LOG_FILE 
#
#
#Step 4. Set permissions 2>&1 >> $LOG_FILE 
echo "Set /data/CDS_DATA permissions" 2>&1 >> $LOG_FILE 
chmod -R 0700 /data/CDS_DATA 2>&1 >> $LOG_FILE 
chown -R cdsdb.cdsdb /data/CDS_DATA 2>&1 >> $LOG_FILE 
#
#
#Step 5. su to cdsdb 2>&1 >> $LOG_FILE   
su - cdsdb 2>&1 >> $LOG_FILE 
#
#
#Step 6. Move data file 2>&1 >> $LOG_FILE 
echo "Move /opt/co/CDSDB_ROOT/DATA to /data/CDS_DATA" 2>&1 >> $LOG_FILE 
/bin/mv /opt/co/CDSDB_ROOT/DATA/* /data/CDS_DATA 2>&1 >> $LOG_FILE 
#
#
#Step 7. Remove /opt/co/CDSDB_ROOT/DATA file 2>&1 >> $LOG_FILE 
/bin/rm -rf /opt/co/CDSDB_ROOT/DATA 2>&1 >> $LOG_FILE 
#
#
#Step 8. Create symlink to new data location 2>&1 >> $LOG_FILE 
ln -s /data/CDS_DATA /opt/co/CDSDB_ROOT/DATA 2>&1 >> $LOG_FILE  
#
#
#Step 9.su to root 2>&1 >> $LOG_FILE 
sudo su 2>&1 >> $LOG_FILE 
#
#
#Step 10. Set permissions 2>&1 >> $LOG_FILE 
chmod -R 0700 /opt/co/CDSDB_ROOT/DATA 2>&1 >> $LOG_FILE 
#
#
#Step 13. "Verify Information - If information is correct, press any key to reboot
#system, otherwise press ctrl-c to break" 
read -p "Everything OK? If yes, press any key to reboot system, otherwise press ctl-c to break" 2>&1 >> $LOG_FILE 
/sbin/reboot 2>&1 >> $LOG_FILE

Open in new window

Avatar of mccracky
mccracky
Flag of United States of America image

You need as the first line in the script:

#!/bin/bash
didn't read that correctly.

You can't use sudo su - in a script.

You can't just run the script itself as root?

sudo script.sh
Avatar of missymadi
missymadi

ASKER

it's there I just took the main part of the script out because it had company info on it. Everything is there that is needed.
I had the steps  5-10 on one line. But my co workers thought it would be easier to read broken out.  
I'm not sure what you mean...here is how I'm running the script
on konsole, su <enter> then enter the password for su. Then run the script /home/usr/desktop/DataMove.sh <enter>
If you are already running the script as the root user, then why do you have step two (and nine) in there?
Avatar of farzanj
You have to run this script as an ordinary user

Or, you have to comment out the lines that says sudo su -

Or, you have to add root user to sudo as well so that it can re-login to itself using sudo
ASKER CERTIFIED SOLUTION
Avatar of wesly_chen
wesly_chen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Your 2>&1 > redirection is perfectly good, as long as there is a space before 2>
farzanj.
2>&1 >file

Open in new window


does not have the same affect as

>file 2>&1

Open in new window


For easier logging, change
LOG_FILE='/data/CDS_DATA/PS_DBInstall.log'2>&1 >> $LOG_FILE 

Open in new window


to

LOG_FILE=/data/CDS_DATA/PS_DBInstall.log
exec >>$LOG_FILE 2>&1

Open in new window


Then you can get rid of all the redirection in the rest of the script, eg:

echo "Backup and copy the environment profile" 2>&1 >> $LOG_FILE 

Open in new window


simply becomes

echo "Backup and copy the environment profile" 

Open in new window



Make it straight forward
--------------
#!/bin/bash

#Logging:
#
#
LOG_FILE='/data/CDS_DATA/PS_DBInstall.log'

#
#
#Step 1. Create a profile backup. Replace current profile file with updated file.

echo "Backup and copy the environment profile"
/bin/cp /etc/profile /etc/profile.bak >> $LOG_FILE 2>&1
/bin/cp -f /home/tcdl/Desktop/profile /etc/profile >> $LOG_FILE 2>&1
     
#
#
#Step 2. Create a directory
echo "Make /data/CDS_DATA directory"
mkdir /data/CDS_DATA >> $LOG_FILE 2>&1
#
#
#Step 3. Set permissions
echo "Set /data/CDS_DATA permissions"
chmod -R 0700 /data/CDS_DATA >> $LOG_FILE 2>&1
chown -R cdsdb.cdsdb /data/CDS_DATA >> $LOG_FILE 2>&1

#
#
#Step 4. Move data file 2>&1 >> $LOG_FILE as cds
echo "Move /opt/co/CDSDB_ROOT/DATA to /data/CDS_DATA"
su - cds -c "/bin/mv /opt/co/CDSDB_ROOT/DATA/* /data/CDS_DATA" >> $LOG_FILE 2>&1  
#
#
#Step 5. Remove /opt/co/CDSDB_ROOT/DATA file
echo "Remove /opt/co/CDSDB_ROOT/DATA"
/bin/rm -rf /opt/co/CDSDB_ROOT/DATA >> $LOG_FILE 2>&1
#
#
#Step 6. Create symlink to new data location
echo "Create symlink to new data location"
su - cds -c "ln -s /data/CDS_DATA /opt/co/CDSDB_ROOT/DATA" >> $LOG_FILE 2>&1  

#
#
#Step 7. Set permissions
echo "Set permissions"
chmod -R 0700 /opt/co/CDSDB_ROOT/DATA  >> $LOG_FILE 2>&1
#
#
#Step 8. "Verify Information - If information is correct, press any key to reboot
#system, otherwise press ctrl-c to break"
read -p "Everything OK? If yes, press any key to reboot system, otherwise press ctl-c to break"
/sbin/reboot >> $LOG_FILE 2>&1
When you execute "sudo su ~", you create a new shell.  It doesn't change the permissions on the current shell.  The shell command which you invoked will wait until you exit from the new shell before continuing on to execute the next commands.  

If you want to execute a command as root, run it as "sudo <cmd>".