Lab 14
Part 1:
First thing you need to do is create some files that have spaces in the name and some files that do not have spaces:
mkdir playground
cd playground
for i in `seq 1 10`; do touch "file $i.txt"; done;
for i in `seq 11 20`; do touch "file$i.txt"; done;
ls
You've been provided with a lot of help below, but you will still be required to think and figure out the final script to submit. So go through the provided exercises and then refine your code to complete the assignment.
Here is the pseudocode to help you solve the problem:
1. loop through all the files in the directory
- By default scripts are aware of all their siblings in their parent directory. So a standard loop through all the files looks like this:
- Run this on the command line and see what it does. Remember that semicolons separate each command:
- for file in *; do echo "$file"; done
2. grep for space in the filename
- Next you need to grep for a space - we should all know how to grep inside a file, but we need to grep the filename itself meaning that we need to echo the filename ($file) and then pipe (|) to grep. Grepping for a space is simply: " ".
- Run your new command on the command line and see what it does (make sure you have filenames with spaces in them):
- for file in *; do echo "$file" | grep " "; done
3. capture results of grep operation ($?)
- When we run a command it actually has a hidden result that it returns to us. This return is usually a 0 (success) or a 1 (failure). We can capture this result by using the $? variable.
- results=$? (remember no spaces when we set a new variable)
- Run your new command on the command line and see what it does:
- for file in *; do echo "$file" | grep " "; results=$?; echo $results; done
4. Do an if test to see if it did have a space
- This is simply an if statement using our newly created results variable.
- if [ $results -eq 0 ]; then ...; fi (You should know from the previous step that 0 means successful and it did find a space)
- Spacing is important inside the brackets.
- Run your new command on the command line and see what it does. It should echo each filename twice -- once for the grep command and once for if statement:
- for file in *; do echo "$file" | grep " "; results=$?; if [ $results -eq 0 ]; then echo "$file"; fi; done
- Notice that on both instances of $file that I've surrounded it with quotation marks. The reason for this is because our "file" has a space in the name. When running commands on filenames with spaces - bash will interpret these names as multiple arguments instead of just one filename. This is why we don't put spaces in the filenames.
- It won't make a difference on the echo command, but other commands will have troubles. So if there is a chance of misinterpretation, it's a good habit to surround our variable values in quotations. `"$file"`.
---------------This is the part where you get to think------------------
Everything you write will go inside of the if statement:
for file in *; do echo "$file" | grep " "; results=$?; if [ $results -eq 0 ]; then <put your code here> fi; done
5. If it did, echo the filename out and use sed to replace spaces with underscores.
- For this step you need to pipe your echo command to the sed search and replace command (write this yourself) and save the results in a variable such as underscore=`echo $file | sed '...'`. Remember you will need backticks (upper left on the keyboard) to actually execute the code before it saves it in the variable.
- Add in an echo $underscore just to make sure it does what you want it to.
6. Use mv command to rename file
- As soon as your $underscore variable is working correctly
- using your two variables $underscore and $file write a move command that renames $file to $underscore
7. Echo out a nice success message to the user
- $file has been renamed to $underscore
---------------------------------------------------------------------------------
8. End if, end loop
Don't forget to test it.
Now that you have working code on the command line - put it in a script file. (myscript.sh)
Don't forget your shebang! (#!/bin/bash)
Make your file executable (chmod) and run it. (./myscript.sh)
You may have to create new files with underscores to properly test your completed script. Make sure you are still in your playground directory.
- rm *.txt; ls
- for i in `seq 1 10`; do touch "file $i.txt"; done;
- ./myscript.sh
Upload a .txt copy or a screenshot of your working code.
To pass off
Submit a screenshot of your script and proof that it worked.