Skip to content

Instantly share code, notes, and snippets.

@harrietty
Last active November 13, 2018 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harrietty/a21ca7a0a223107f921947f66f0b3e46 to your computer and use it in GitHub Desktop.
Save harrietty/a21ca7a0a223107f921947f66f0b3e46 to your computer and use it in GitHub Desktop.

Excersises for learning bash shell scripting

Part 1: Creating scripts and simple logic

1. Create a program that tells you whether the argument you give it is a file, a directory or does not exist.

Use: $ ./test.sh foo

Output:

foo does not exist

foo is a directory

foo is a file


2. Create a program that takes two numbers as arguments (check they are numbers) and tells you which is largest. It should tell you if you pass incorrect arguments or not enough arguments.

Use:

$ ./check.sh 4 183

Output:

foo is not a number

please provide 2 arguments

183 is larger than 4

3. Create a program that takes a filename as an argument and tells you whether it is executable. If it is a directory not a file, the program should tell you. It should also tell you if the file does not exist.

Use:

$ ./check.sh bar.txt

Output:

bar is a directory

bar does not exist

bar is executable

4. Create a program that takes two strings as arguments and tells you which is longer.

Use:

$ ./count.sh hello everyone

Output:

please provide 2 arguments

everyone is longer than hello

5. Create a program that takes the name of a file and tells you whether it includes the date in the filename in the format DD-MM-YYYY. You might want to check for nonsensical dates (e.g. 44-66-2018) but no need to take this too far!

Use:

$ ./check.sh reports_15-04-2017.txt

Output:

please provide an argument

reports_15-19-2019.txt does not contain a valid date

reports-10:04:2016 does not contain a valid date

reports_15-04-2017.txt contains a valid date

Part 2: Working with commands, files and directories

In this section you will have to use the tools from the first section, along with:

  • Piping and redirection
  • Command substitution
  • Arithmetic in bash
  • Working with files and directories

1. Create a script that takes a file full of space-separated words and prints the words separated by line, ordered alphabetically. It should also check that the file exists.

Use: $ ./separate.sh words.txt

Output:

words.txt does not exist

apple
feather
horse
pinecone

2. Create a program that takes a file full of comma-separated numbers and prints the largest number. It should also check that the file exists

Use: $ ./largest.sh results.txt

Output:

results.txt does not exist

5604

3. Create a program which takes a file of comma-separated first names and delivers a report of the top 5 names by frequency.

Use: $ ./largest.sh votes.txt

Output:

foo.txt is not a file

124 Julia
87 Tony
57 Marsha
34 John
33 Abdul

4. Create a program that counts the number of files and directories (one level only) in a directory. It should also check that the argument given is a directory.

Use:

$ ./count.sh foo

Results:

foo is not a directory

foo contains 5 files/directories

5. Create a program that counts the number of files in 2 directories and tells you which has the most. It should also check that the arguments given are directories.

Use: $ ./count.sh foo bar

Results:

foo has the most files (4)

foo and bar both have 3 files

6. Create a program that creates a new executable file with the given name. The script must add executable permission for the user and should add the shebang (#!/bin/bash) to the top line of the file

Use: $ ./create.sh foo

Results:

foo already exists

Success! Executable file foo.sh created

7. Create a script that tells you the number of characters in a file. You will have to work around the fact that a newline (\n) character will exist at the end of each line. You don't want to include these in your count.

Use: $ ./count bar.txt

Results:

bar is not a file

bar contains 1 character

bar contains 89234 characters

8. Create a script that tells you the difference in length (of total characters) between two files. It should also check that both files exist.

Use: $ ./check.sh foo bar

Results:

file foo does not exist

files foo and bar both have 100 characters

file foo has 23 more characters than file bar

9. Create a script that tells you how many files and how many directories are in a given directory.

Use: $ ./count.sh foo

Results:

foo is not a directory

foo contains 0 files and 0 directories

foo contains 12 files and 3 directories

10. Create a script that tells you which file/directory in a given directory is the largest in bytes. Don't worry about whether there is more than 1 file of the same size - just print whatever comes first, for now. The script should check the argument is a directory and also that it contains more than 1 item.

Use: $ ./largest.sh foo

Results:

foo is not a directory

foo does not contain any items

foo/build is the largest file

11. Create a script that takes a directory and a number (n) and prints the n largest files/directories by bytes in the directory (ignoring files in any sub-directories). It should also check that the directory exists and that n is provided and is a number.

Use: $ ./largest.sh foo 5

Results:

please provide 2 arguments

foo is not a directory

Incorrect argument: bar is not a number

2940	src/main.js
324	src/app.js

12. Create a script that tells you which JavaScript file (.js) is the largest in bytes in the directory given. Don't worry about whether there is more than 1 file of the same size - just print whatever comes first, for now. It should also tell you how many bytes.

Use: $ ./largest foo

Results:

foo is not a directory

foo does not contain any files

no .js files found

foo/build/bundle.js is the largest file at 72947 bytes

13. Create a script that takes 2 directories and tells you how many files/directories of the same name exist in both directories. It should also check that the directories exist and are directories.

Use: $ ./duplicates.sh foo bar

Results:

foo does not exist

bar is not a directory

foo and bar have 4 file/directory names in common

14. Create a script that counts how many sub-directories (and further nested directories) are inside a given directory. It should also check that the directory exists.

Use: $ ./count.sh foo

Results:

foo is not a directory

foo contains 3455 nested directories

Input and output

For these exercises we will also need to use:

  • handling input and output
  • working with and redirecting the standard streams
  • control flow with for, while and until loops

  1. Create a script that asks a user for a filename and creates a file with this name. It should also ask whether the user wants to have write and execute permission, and update the permissions accordingly. If you ask a user for a Y/N answer to the question "Do you want to have write permissions? Y/N" and they respond with unexpected input, your program should keep asking until it receives input it understands.

  1. Create a program that reads from a file of space-separated columns of names and scores and prints them in a neat table. The program should ask the user how wide, in spaces, it would like each scores column in the table to be (the first column should be 15 characters wide).

Example input:

grades.txt

Amanda 55 78 79 67 81
Heather 76 56 45 76 42
Ben 76 44 54 76 78
Penelope 82 79 76 75 83

Output:

Amanda         55    78     79     67     81
Heather        76    56     45     76     42
Ben            76    44     54     76     78
Penelope       82    79     76     75     83
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment