Writing a script in 2 places: $ & #


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Writing a script in 2 places: $ & #
# 1  
Old 07-08-2005
Question Writing a script in 2 places: $ & #

Hi all, first time here, but I figured you'd be the ones to ask...

I want to write a scrip which starts in shell ' $ ' and give it a command, then change to superuser and give it more commands. Then exit su, and give it more commands at '$' Is this possible?

Ex:

**************************************************
#!/bin/sh
echo "Hello, in $"

su root # it will ask for a password now, how do I give it to it?
???????? #password

# now in su mode at prompt #

echo "Hello, in #"
ls
#and some more commands

exit #leave su mode
#come back to '$'

echo "back to $"
echo "I'm done."

**********************************************

Any help would greatly appreciated

Thanks

~J
# 2  
Old 07-08-2005
If you really want it to do just some certain commands as root, then put those into a second script and run the su as shown:
su - root -c "/usr/local/bin/otherjunk"

If you are running this from a terminal, then it WILL ask for the password. If you are trying to make it work in say cron, then it will not.

So your first script:
#!/bin/sh
echo "Hello, in $"
su - root -c "/usr/local/bin/otherjunk"
echo "back to $"
echo "I'm done."

Your second script might be able to just have the commands you wanted to run:
echo "hello, in $SHELL as `id`"
ls
exit


Otherwise, you would need something like expect to run what you are looking for.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sum the fields with 6 decimal places - getting only 2 decimal places as output

I used the below script to Sum up a field in a file based on some unique values. But the problem is when it is summing up the units, it is truncating to 2 decimals and not 6 decimals as in the input file (Input file has the units with up to 6 Decimals – Sample data below, when the units in the 2... (4 Replies)
Discussion started by: brlsubbu
4 Replies

2. Shell Programming and Scripting

SFTP Shell Script Get & Delete && Upload & Delete

Hi All, Do you have any sample script, - auto get file from SFTP remote server and delete file in remove server after downloaded. - only download specify filename - auto upload file from local to SFTP remote server and delete local folder file after uploaded - only upload specify filename ... (3 Replies)
Discussion started by: weesiong
3 Replies

3. Solaris

Sync script between two places

Hello Team, We would like to copy or sync a couple a zpool and all dataset inside between two datacenters one time per day, the structure and space are the same in both sides. We are consider to use rsync and a cron job. We would like to know what type of ports we have to open in the... (1 Reply)
Discussion started by: csierra
1 Replies

4. Shell Programming and Scripting

Linux Platform - NDM Script - && Operator

Hi All, I have a requirement where i need to NDM 3 files from LINUX to Mainframe system & trigger a job in mainframe once the 3 files are transmitted successfully. I am getting an error message in the && operator (the code component where i am checking whether step 1/2/3 are completed). ... (2 Replies)
Discussion started by: dsfreddie
2 Replies

5. Shell Programming and Scripting

[Shell/Perl(?)] Prepending timestamps to console output & writing results to a file

I do a lot of TSM work and I embarked on what I thought would be an easy task, and I'd be very happy for any input to save the pounding my keyboard is receiving :] By default, the output of TSM's console has no timestamping, making it hard to sort through accurately. This puts my console into... (5 Replies)
Discussion started by: Vryali
5 Replies

6. Shell Programming and Scripting

Using Grep & find & while read line in a script

Hello people! I would like to create one script following this stage I have one directory with 100 files File001 File002 ... File100 (This is the format of content of the 100 files) 2012/03/10 12:56:50:221875936 1292800448912 12345 0x00 0x04 0 then I have one... (0 Replies)
Discussion started by: Abv_mx81
0 Replies

7. Shell Programming and Scripting

Difference of two data files & writing to an outfile.

Hi Everyone, I have two files i.e. one file2 contains today's data and the other file1 contains Yesterday's data. The data in the files contains 226 columns and the data for the coulums separated by a Pipe "|" delimiter. Now, I have 4 Primary keys (coulumns) by which I have to compare file2 and... (10 Replies)
Discussion started by: filter
10 Replies

8. Shell Programming and Scripting

Get the places of binary digits in the korn shell script

TO THE ALMIGHTY FORUM , though i have already posted the same question on hex to binary thread , i am posting here also for other beginners who may benefit from this thread... I have a 32 bit binary containing a series of 1' and 0's , and i am stuck... (2 Replies)
Discussion started by: venu
2 Replies

9. UNIX for Dummies Questions & Answers

Problem with xterm & tcsh & sourcing a script in a single command

Hi friends, I have a script that sets the env variable path based on different conditions. Now the new path variable setting should not done in the same terminal or same shell. Only a new terminal or new shell should have the new path env variable set. I am able to do this only as follows: >cd... (1 Reply)
Discussion started by: sowmya005
1 Replies

10. Shell Programming and Scripting

Delete lines at several places in a file with script

Hi, I am a newbie to shell scripting, and I have a file which quite large which I would like to delete lines at certain places. I want to search for a keyword which is recurring in the file. When matched I would like to delete the line. And when the file was so huge I thought I ought to learn... (3 Replies)
Discussion started by: mr_andrew
3 Replies
Login or Register to Ask a Question