LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-20-2005, 08:48 AM   #1
johnnybhoy67
Member
 
Registered: Sep 2004
Distribution: Knoppix 4.0.2
Posts: 54

Rep: Reputation: 15
How do I script these commands?


Hi All,

I'd like to be able to script these commands but don't know how to write shell scripts, can anyone offer a simple example?

Cheers

J

su root
parted -s /dev/hda mkpartfs primary fat32 1 8000
cat /proc/partitions

cd /UNIONFS/mnt
mkdir fat32
chown knoppix fat32
chmod 755 fat32

mount -0 rw,umask=0, /dev/hda1 /UNIONFS/mnt/FAT32

ifconfig eth0 add 130.209.13.209 netmask 255.255.255.0
 
Old 10-20-2005, 09:05 AM   #2
Keruskerfuerst
Senior Member
 
Registered: Oct 2005
Location: Horgau, Germany
Distribution: Manjaro KDE, Win 10
Posts: 2,199

Rep: Reputation: 164Reputation: 164
Hello!

---------------------------script beginning

#!/bin/bash
parted -s /dev/hda mkpartfs primary fat32 1 8000
cat /proc/partitions

cd /UNIONFS/mnt
mkdir fat32
chown knoppix fat32
chmod 755 fat32

mount -0 rw,umask=0, /dev/hda1 /UNIONFS/mnt/FAT32

ifconfig eth0 add 130.209.13.209 netmask 255.255.255.0
 
Old 10-20-2005, 09:12 AM   #3
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
At its simplest scripting can just be putting everything you do into a file and making it an executable by using the chmod command to add the execute bit.

Therefore you could just add everything you typed below into a single file and try it but HOLD ON. It won't work but ONLY because you're doing the "su" first. When you do "su" you're actually starting a new shell so none of the commands following would be exeucted until AFTER you'd exited the "su".

Due to above you just need to modify it slightly. Make 2 scripts.

The first one would include everything below your "su root".

The second one would be a one liner that does:

su root -c <firstscript> (or su root --command=<firstscript>) where <firstscript> is the actual name of the first script you'd created.

The "-c" says to execute the command that follows which in this case would be the name of the first script you'd created.

You SHOULD but don't have to include the shell directive at the beginning of the script. This is only so the script always uses the shell you expect regardless of the shell the invoking user is running. The shell directive for bash would be:

#!/bin/bash

Note that the "#" is required - this is a special non-comment usage. Also note that this MUST be the first line of the script.

Finally I note that you wrote "su root". If you do that it does not get root's environment - it inherits the invoking user's environment. You should probably do "su - root" instead to be sure it always has root's environment.
 
Old 10-21-2005, 08:34 AM   #4
johnnybhoy67
Member
 
Registered: Sep 2004
Distribution: Knoppix 4.0.2
Posts: 54

Original Poster
Rep: Reputation: 15
Hi Guys,

Thanks for this.

jlightner thanks for such a comprehensive answer!! I often find it difficult to understand WHY certain things have to be done in a certain way and being a relative n00b to Linux it's all still very confusing....but fun :-)

Many Thanks

Johnnybhoy
 
Old 10-21-2005, 09:20 AM   #5
johnnybhoy67
Member
 
Registered: Sep 2004
Distribution: Knoppix 4.0.2
Posts: 54

Original Poster
Rep: Reputation: 15
Hi jlightner,

I've done as you suggest but I get the error " : bad interpreter: No such file or directory"

I suppose this would be a good time to mention that this is a knoppix live CD but that shouldn't make a difference I don't think as /bin/bash exists.

Any ideas?

Cheers

J
 
Old 10-21-2005, 09:42 AM   #6
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
From your error it appears its finding a space or a null.

Make sure the directive line is the TOP LINE in the file and that there are no spaces in it:

#!/bin/bash

As noted previously the # (number/pound sign) is required as is the ! (exclamation point).

Type "ps" and see what shell you are using. If you're already using bash you can leave out the directive from your script. By the way you don't need it it the script that is doing the su as it is invoking a new shell based on user setup. ("grep root /etc/passwd" to see which shell root has - it's at the end of the line).

Try typing "/bin/bash" at the command line and see if it gives you a new shell. If so then just type exit to go back to your original one. This will at least confirm it is finding /bin/bash and executing it.

I'm not familiar with the knoppix live CD - I guess its possible its not allowing for this but I wouldn't think so since it is fairly basic operation of shell scripting.
 
Old 10-21-2005, 09:53 AM   #7
johnnybhoy67
Member
 
Registered: Sep 2004
Distribution: Knoppix 4.0.2
Posts: 54

Original Poster
Rep: Reputation: 15
ok getting a bit further now.....

Removed the #!/bin/bash from teh top line and I now get prompted for the root password. But I don't want to be prompted for the password, is there any way around this??

Cheers

John
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
console commands as script? rooman Linux - Newbie 14 02-02-2006 07:49 PM
Script to repeat commands satimis Programming 8 08-12-2005 04:03 AM
commands behind a bash here script onurbi Linux - Software 3 11-14-2003 07:38 AM
run 2 commands in a script ddpicard Linux - General 10 06-13-2003 04:50 PM
Using vi commands in a bash script tiger Programming 1 05-21-2001 05:08 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration