[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-04-19
11
Medium Priority
?
412 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:missymadi
  • 3
  • 2
  • 2
  • +3
11 Comments
 
LVL 12

Expert Comment

by:mccracky
ID: 35427057
You need as the first line in the script:

#!/bin/bash
0
 
LVL 12

Expert Comment

by:mccracky
ID: 35427088
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
0
 

Author Comment

by:missymadi
ID: 35427100
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.  
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

Author Comment

by:missymadi
ID: 35427126
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>
0
 
LVL 12

Expert Comment

by:mccracky
ID: 35427183
If you are already running the script as the root user, then why do you have step two (and nine) in there?
0
 
LVL 31

Expert Comment

by:farzanj
ID: 35427404
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
0
 
LVL 38

Accepted Solution

by:
wesly_chen earned 2000 total points
ID: 35427474
> LOG_FILE='/data/CDS_DATA/PS_DBInstall.log'2>&1 >> $LOG_FILE
LOG_FILE='/data/CDS_DATA/PS_DBInstall.log'

1. Please use the script in
http://www.experts-exchange.com/OS/Linux/Q_26950591.html#35419073

2. No "sudo su -" or "su -" inside the script. "su - cds -c <command>" inside the script is ok.

3.
Don't use 2>&1 >> $LOG_FILE inside /home/usr/desktop/DataMove.sh
Run it this way:
$ sudo su -
# /home/usr/desktop/DataMove.sh > /data/CDS_DATA/PS_DBInstall.log 2>&1
0
 
LVL 31

Expert Comment

by:farzanj
ID: 35427505
Your 2>&1 > redirection is perfectly good, as long as there is a space before 2>
0
 
LVL 48

Expert Comment

by:Tintin
ID: 35427947
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



0
 
LVL 38

Expert Comment

by:wesly_chen
ID: 35428052
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
0
 
LVL 8

Expert Comment

by:eager
ID: 35435591
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>".  
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses
Course of the Month18 days, 6 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question