LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   My first attempt at a shell script (https://www.linuxquestions.org/questions/programming-9/my-first-attempt-at-a-shell-script-910956/)

Trouble31 10-30-2011 02:33 PM

My first attempt at a shell script
 
My VMware will not run today so I can't try running anything. Can someone look at this and tell me what I need to work on please? I am just looking for someone to point me in the right direction.

Instructions: Write the following shell script called Exercise8 and insert it below:
Write a shell script using a bash shell that creates the following directories and
• Homework directory on the Knoppix home directory
• First directory under Homework directory
• Second directory under Homework directory
Copies the following files to the directories indicated:
• Proposal.txt on the floppy at /media/fd0 to Homework directory
• Readme.txt on the floppy at /media/fd0 to First directory in the Homework directory

First attempt:

#!/bin/bash

# mkdir /home/user/$Homework
# mkdir –p /home/user/Homework/First
# mkdir –p /home/user/Homework/Second

# cd /mnt/floppy
# cp /media/Proposal.txt /home/user/Homework
# cp /media/Readmel.txt /home/user/Homework/First
# sync
# cd /
# umount /mnt/floppy

#exit 0

SigTerm 10-30-2011 02:38 PM

Quote:

Originally Posted by Trouble31 (Post 4511987)
My VMware will not run today so I can't try running anything. Can someone look at this and tell me what I need to work on please? I am just looking for someone to point me in the right direction.

You need Knoppix. You don't need vmware to run knoppix. Download Knoppix iso, burn it onto CD or DVD, and boot the CD/DVD. Then do the homework.

TobiSGD 10-30-2011 02:41 PM

The first three lines after the shebang can be compressed to one
Code:

mkdir -p /home/user/Homework/{First,Second}
I wonder about this one:
Code:

cd /mnt/floppy
What do you want to achieve here? You don't have to change directories to copy anything as long as you don't use relative paths (which you are not doing), so you can omit that line (besides that you are changing to the wrong directory). You also don't need the second cd-command.

Also keep in mind that the umount-command normally only works for the root-user.

Trouble31 10-30-2011 02:42 PM

I have Knoppix, it has been running for a few weeks and now today it says the software isn't installed. The ISO CD is still in my DVD drive. The only thing I did was reboot my laptop. VMware is what I have been using to run Knoppix because I don't have enough hard drive room to install it.

sycamorex 10-30-2011 02:44 PM

Code:

#!/bin/bash

# mkdir /home/user/$Homework
# mkdir –p /home/user/Homework/First
# mkdir –p /home/user/Homework/Second

# cd /mnt/floppy
# cp /media/Proposal.txt /home/user/Homework
# cp /media/Readmel.txt /home/user/Homework/First
# sync
# cd /
# umount /mnt/floppy

Please post your code in code tags.
Why do you start each line with a #? Do you know its function in BASH?

There's a way of writing all 'mkdir' commands in one.

Unless specifically instructed you don't need to cd to a directory from where you want to copy stuff.


Code:

# cd /mnt/floppy
# sync
# cd /
# umount /mnt/floppy


Why did you write the above? I mean it doesn't seem to be in your task instructions.

sycamorex 10-30-2011 02:48 PM

Quote:

Originally Posted by Trouble31 (Post 4511996)
I have Knoppix, it has been running for a few weeks and now today it says the software isn't installed. The ISO CD is still in my DVD drive. The only thing I did was reboot my laptop. VMware is what I have been using to run Knoppix because I don't have enough hard drive room to install it.


You don't usually install Knoppix. It funcions as a Live CD, which means that you boot your computer from the Knoppix CD/DVD and it loads to ram memory so your current installation on the hard drive stays intact.

Trouble31 10-30-2011 02:54 PM

So I want something more like this?

#!/bin/bash

mkdir -p /home/user/Homework/{First,Second}
cd /mnt/floppy
cp /media/Proposal.txt /home/user/Homework
cp /media/Readmel.txt /home/user/Homework/First

exit 0

sycamorex 10-30-2011 02:58 PM

Again, place your code in code tags (Go Advanced)


Why do you cd /mnt/floppy?

TobiSGD 10-30-2011 03:01 PM

Haven't seen that in your first attempt: The instructions state the the floppy is mounted at /media/fd0, so you have to copy your files from /media/fd0/ instead of /media/.

Trouble31 10-30-2011 03:17 PM

I'm still trying to work on the code tags. There is NOTHING in my handouts about them so I am scouring the web.

Latest version

#!/bin/bash

mkdir -p /home/user/Homework/{First,Second}

cp /media/fd0/Proposal.txt /home/user/Homework
cp /media/fd0/Readmel.txt /home/user/Homework/First

exit 0

Nylex 10-30-2011 03:26 PM

Quote:

Originally Posted by Trouble31 (Post 4512019)
I'm still trying to work on the code tags. There is NOTHING in my handouts about them so I am scouring the web.

They're a feature of vBulletin forums, such as this one. You simply put your code between [code] and [/code]. You can also (as sycamorex pointed out) press the "Go Advanced" button and then use the button with a '#' on it to put them in.

sycamorex 10-30-2011 03:28 PM

Quote:

Originally Posted by Trouble31 (Post 4512019)
I'm still trying to work on the code tags. There is NOTHING in my handouts about them so I am scouring the web.

Latest version

#!/bin/bash

mkdir -p /home/user/Homework/{First,Second}

cp /media/fd0/Proposal.txt /home/user/Homework
cp /media/fd0/Readmel.txt /home/user/Homework/First

exit 0

What I meant by code tags are the tags HERE on the Linux Questions forum. Code tags are used for... code to preserve the formatting and make it more readable. Compare the two versions of the same code (the code is not relevant here):

With Code tags:
Code:

(defun numbersp (lst)                                                                         
          (cond                                                                                       
            ((null lst) T)                                                                             
            ((numberp (car lst)) (numbersp (cdr lst)))))


Without code tags:

(defun numbersp (lst)
(cond
((null lst) T)
((numberp (car lst)) (numbersp (cdr lst)))))

Cedrik 10-30-2011 05:41 PM

Quote:

the Knoppix home directory
Is it really /home/user ? not /home/knoppix ?


All times are GMT -5. The time now is 12:59 AM.