Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Excersises for learning bash shell scripting
- ## Easy
- #### 1. Create a program that counts the number of files in a directory. It should also check that the argument given is a directory.
- Use:
- `$ ./count foo`
- Results:
- `$ foo is not a directory`
- `$ There are 5 files in foo`
- #### 2. 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 foo bar`
- Results:
- `$ foo has the most files (4)`
- `$ foo and bar both have 3 files`
- #### 3. 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 foo`
- Results:
- `$ foo already exists`
- `$ Success! Executable file foo.sh created`
- #### 4. Create a script that tells you the difference in length between two files. It should also check that both files exist.
- Use:
- `$ ./check 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`
- #### 5. Create a script that tells you which file in a directory has the longest name. The script should check the argument is a directory and also that it contains more than 1 file.
- Use:
- `$ ./count foo`
- Results:
- `$ foo is not a directory`
- `$ foo does not contain any files`
- `$ hello_world.txt has the longest filename in foo`
- #### 6. Create a script that takes 2 directories and tells you how many filenames of the same name exist in both directories. It should also check that the directories exist and are directories.
- Use:
- `$ ./duplicates foo bar`
- Results:
- `$ foo does not exist`
- `$ bar is not a directory`
- `$ foo and bar have 4 files in common`
- #### 7. 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 words.txt`
- Results:
- `$ words.txt does not exist`
- ```
- apple
- feather
- horse
- pinecone
- ```
Add Comment
Please, Sign In to add comment