MoreBash:Exercises - Scripts - Introduction

From Juneday education
Jump to: navigation, search


Getting started

Our first script

Create a new file, getting-started.sh and add the following content:

#!/bin/bash

echo "Hi there!"

Try to execute it, by typing:

getting-started.sh

You'll most likely see something like:

$ getting-started.sh
bash: getting-started.sh: command not found...

This means that the shell (in the terminal) we're running can not find the script getting-started.sh. We need to specify where the script is located:

$ ./getting-started.sh

Now you'll probably see something like:

$ ./getting-started.sh
bash: ./getting-started.sh: Permission denied

Arghh, still not working. We don't have permissions to execute thescript. Let's add permissions:

$ chmod a+x getting-started.sh

And now, let's try one more time to execute the script:

$ ./getting-started.sh
Hi there!

Yes, finally.

Simple scripts

Date script

Write a script that prints the current date (using the date command).


Expand using link to the right to see a hint/solution.

Create a (text) file, typically called date.sh with the following content.

#!/bin/bash

date

Since this file now contains commands that bash can interpret we this a script. Don't forget to make the script executable:

chmod a+x date.sh

Time script

Write a script that prints the current time (using the date command).

Note: the current date shall not be printed.

Expand using link to the right to see a hint/solution.

Create a (text) file, typically called time.sh with the following content.

#!/bin/bash

date '+%T'

Date and Time script

Write a script that prints the current date and time (using the date command) in the default format.

Expand using link to the right to see a hint/solution.

Create a (text) file, typically called date-time.sh with the following content.

#!/bin/bash

date +"%Y-%m-%d"


Date and Time script formated

Write a script that prints the current date and time (using the date command) in the following format "yyyy-mm-hh hh:mm:ss".

Expand using link to the right to see a hint.

Check the manual .

GNU/Linux and MacOS users can also type man date.

Expand using link to the right to see a solution.

Create a (text) file, typically called date-time-format.sh with the following content.

#!/bin/bash

date +"%Y-%m-%d %H:%m:%S"

Trouble-shooting

If you get this error from running a script:

syntax error near unexpected token `$'{\r''

...then you probably have modified the script in a Windows editor so that it got CRLF line endings (the Windows style line endings).

You can convert the file (any text file, actually) from windows format to Unix format using dos2unix (install it in Cygwin if you are running Cygwin). If you are using a text editor, you can always change the line endings in the file:

Atom: Look for the letters CRLF at the bottom of the editor, click on it and change it to LF only.

Notepad++: Edit -> EOL Conversion -> Unix Format

Links

Chapter links

previous (Scripts - Introduction) | next (Declare - Typing variables)