Getting The Source Path
Part of the Fundamentals section of Coddy's Terminal journey — lesson 75 of 82.
Challenge
EasyLet's start building the backup script! The first step is to create a script that asks the user for the source directory they want to back up.
Create a shell script called backup.sh that:
- Starts with the shebang line (
#!/bin/bash) - Uses
read -pto prompt the user with:Enter source directory: - Stores the user's input in a variable called
source_dir - Prints the message:
Backing up: [source_dir]
Make the script executable. Then run it and enter documents when prompted.
Your final output should be:
Backing up: documentsHint: Use
read -p "Enter source directory: " source_dirto display the prompt and capture input in one line. Remember to usechmod +xto make the script executable before running it with./backup.sh.
Try it yourself
cat server.logAll lessons in Fundamentals
4Directories
Create A DirectoryCopy A DirectoryMove And Rename A DirectoryDelete A DirectoryRecap - Directory Operations7File Content
Head And TailWord CountSort CommandUnique CommandGrep BasicsGrep With FlagsRecap - Text Detective2Navigation
Print Working DirectoryList FilesChange DirectoryAbsolute vs Relative PathsHome And Root DirectoryRecap - Find Your Way8Redirection
Standard OutputOverwrite To A FileAppend To A FileStandard InputStandard ErrorRecap - Log Builder11Permissions
Understanding PermissionsReading PermissionsChmod With NumbersChmod With SymbolsFile OwnershipRecap - Lock It Down14Backup Script Project
Project OverviewGetting The Source Path6Wildcards And Patterns
The Star WildcardThe Question Mark WildcardBracket WildcardsCombining WildcardsRecap - Selective Operations9Piping
What Is A PipeChaining Two CommandsChaining Multiple CommandsPipe With GrepRecap - Data Pipeline