hi, i found this site by accident and am giving it a go for some help needed. I am new to linux and am using Debian Lenny at the moment. I am finding that a lot of my tasks can be made a lot simpler and quicker if i just create a simple script. The problem is i need to know how to write a script. I have a simple idea of a task that i would like to automate with a script:

1. mount /dev/sda1 /mnt/usbdisk
2. rsync -av /mnt/usbdisk/content*.mpg /mpgfiles --progress
3. Wait for the rsync to finish - perhaps show a progress bar
4. When finished chown admin.joe all /mpgfiles/content*.mpg
5. Then chmod 755 all /mpgfiles/content*.mpg
6. rsync -av /mpgfiles/content*.mpg to a remote storage server --progress
7. Display a counter while rsync is in progress
8. after rsync finishes,umount /mnt/usbdisk
9. End

I found a tutorial on this website which I will go through, and I am willing to put in as much effort as is required to learn if someone is willing to teach me and start me off in this simple script. I think it to be in has to be in bash.

Many many thanks in advance.

Copa.

Recommended Answers

All 4 Replies

The thing is...If you can execute the line in a bash terminal then you can include it in a bash shell script

bash.sh

#! /bin/sh

...your steps go here
1. mount /dev/sda1 /mnt/usbdisk
2. rsync -av /mnt/usbdisk/content*.mpg /mpgfiles --progress
3. Wait for the rsync to finish - perhaps show a progress bar
4. When finished chown admin.joe all /mpgfiles/content*.mpg
5. Then chmod 755 all /mpgfiles/content*.mpg
6. rsync -av /mpgfiles/content*.mpg to a remote storage server --progress
7. Display a counter while rsync is in progress
8. after rsync finishes,umount /mnt/usbdisk
....

exit 0

then make the bash shell script executable with chmod +x bash.sh

For example your shell script with the first line could be

bash.sh

#! /bin/sh

mount /dev/sda1 /mnt/usbdisk

exit 0

No no ... it cannot be that simple :)
OK I will commence with the lines that I enter on the CLI ... and build this from there ...
what about points 3 and 7 ?
the rsyncing to be completed before moving to the next point ... will the script wait for the rsyncing to be completed before running the next command ???

I will try it out in any case :)

Many thanks...

Copa.

The thing is...If you can execute the line in a bash terminal then you can include it in a bash shell script

bash.sh

#! /bin/sh

...your steps go here
1. mount /dev/sda1 /mnt/usbdisk
2. rsync -av /mnt/usbdisk/content*.mpg /mpgfiles --progress
3. Wait for the rsync to finish - perhaps show a progress bar
4. When finished chown admin.joe all /mpgfiles/content*.mpg
5. Then chmod 755 all /mpgfiles/content*.mpg
6. rsync -av /mpgfiles/content*.mpg to a remote storage server --progress
7. Display a counter while rsync is in progress
8. after rsync finishes,umount /mnt/usbdisk
....

exit 0

then make the bash shell script executable with chmod +x bash.sh

For example your shell script with the first line could be

bash.sh

#! /bin/sh

mount /dev/sda1 /mnt/usbdisk

exit 0

No no ... it cannot be that simple :)
OK I will commence with the lines that I enter on the CLI ... and build this from there ...
what about points 3 and 7 ?
the rsyncing to be completed before moving to the next point ... will the script wait for the rsyncing to be completed before running the next command ???

I will try it out in any case :)

Many thanks...

Copa.

What you don't how to run the script yourself?
If you run rsync in the foreground then yes the script will wait for it to complete before moving on to the next step..

A script is simply a list of commands that you would type on the command line. It's a text file that starts with a hash-bang (#!) and the program to run it, in this case /bin/bash or /bin/sh .

Every following line is executed in that environment, one at a time. In Bash a line ending in "&" will be executed in the background, otherwise it's in the foreground and the script only proceeds when each item is complete.

To be able to run the script simply add the x tag to the file. After that it can be run with ./scriptname, or you can move it to a directory in your search path like /usr/sbin which allows you to use it from any working directory without specifying the scripts location.

A lot of utilities you are familiar with already are probably just bash scripts sitting in /usr/sbin. Take a look through them.

For available commands, environment variables, etc. read the Bash manpage ( $man bash )

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.