Utah Tech Logo

IT3300

DevOps virtualization

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

2. grep for space in the filename

3. capture results of grep operation ($?)

4. Do an if test to see if it did have a space

 

---------------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.

6. Use mv command to rename file

7. Echo out a nice success message to the user

---------------------------------------------------------------------------------

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.

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.