Question: Script shell problem # Imagine you are the instructor of CSE 15L at UCLA, and you have just finished # the school year of 2016-2017.
Script shell problem
# Imagine you are the instructor of CSE 15L at UCLA, and you have just finished # the school year of 2016-2017. It's that time of year when you wrapped up your # year's work and keep all the old work files archived in an organized fashion. # But when you take a look at your working directory, you have a bad feeling # about this: # # CSE15LFall2015Lecture1028thOct2015.pdf # CSE15LFall2015Lecture112ndNov2015.pdf # CSE15LFall2015Lecture124thNov2015.pdf # CSE15LFall2015Lecture128thSep2015.pdf # CSE15LFall2015Lecture139thNov2015.pdf # CSE15LFall2015Lecture1416thNov2015.pdf # CSE15LFall2015Lecture1518thNov2015.pdf # CSE15LFall2015Lecture1623rdNov2015.pdf # CSE15LFall2015Lecture1725thNov2015.pdf # CSE15LFall2015Lecture1830thNov2015.pdf # CSE15LFall2015Lecture192ndDec2015.pdf # CSE15LFall2015Lecture230thSep2015.pdf # CSE15LFall2015Lecture35thOct2015.pdf # CSE15LFall2015Lecture47thOct2015.pdf # CSE15LFall2015Lecture512thOct2015.pdf # CSE15LFall2015Lecture614thOct2015.pdf # CSE15LFall2015Lecture719thOct2015.pdf # CSE15LFall2015Lecture821stOct2015.pdf # CSE15LFall2015Lecture926thOct2015.pdf # CSE15LSpring2016Lecture1027thApril2016.pdf # CSE15LSpring2016Lecture112ndMay2016.pdf # CSE15LSpring2016Lecture124thMay2016.pdf # CSE15LSpring2016Lecture128thMarch2016.pdf # CSE15LSpring2016Lecture139thMay2016.pdf # CSE15LSpring2016Lecture1416thMay2016.pdf # CSE15LSpring2016Lecture1518thMay2016.pdf # CSE15LSpring2016Lecture1623rdMay2016.pdf # CSE15LSpring2016Lecture1725thMay2016.pdf # # What a mess! Now you wish you had named your slides a little more friendly, # don't you? # # "No worries!" You say to yourself, "I will just use my CSE 15L knowledge to # make my life easy!" # # # Getting Started # --------------- # # We have provided you with a string array. Use this for file matching and # creating directories:
MONTHS=(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
# Your objective is to write a shell script that sorts all slides in the # working directory into sub directories of their corresponding months. That # is, after your script is run, you should be able to find a slide named # CSE15LFall2015Lecture1026thOct2015.pdf under a directory named Oct/, and so # on. # # Notice that if a directory of a month does not exist, you should create it # with mkdir. Otherwise, you should not attempt to make this directory again. # Your script should not have any error output as long as it's run correctly. # # DO NOT ATTEMPT TO HARD CODE YOUR ANSWER. It will be very obvious and sad if # you hard code the files you need to sort, and on top of that, YOU WILL # RECEIVE NO CREDIT. # # Also, MAKE SURE THAT YOUR MONTH DIRECTORIES IS RELATIVE TO YOUR WORKING # DIRECTORY. Your assignment will not be graded under your account, and # therefore ~ will not refer to your home directory, and therefore all paths # following that would very likely not exist. # # # Testing # ------- # # make clean # start with clean slate # make prep # get the slides to current directory # ./script3.sh # run your script # ls -R # make sure that output matches below # # The `make prep` command will prepare your directory by copying all the pdf # slides over to your working directory. Therefore, before you run your script, # make sure you do `make prep` first. # # `ls -R` recursively prints out all the content within the current directory. # You may use this command to check if you script is working correctly. # # Sample result: # # .: # Apr Dec Feb Jan Jun Makefile Mar May Nov Oct Sep hw5.txt script3.sh # # ./Apr: # CSE15LSpring2016Lecture1027thApril2016.pdf # CSE15LSpring2016Lecture34thApril2016.pdf # CSE15LSpring2016Lecture46thApril2016.pdf # CSE15LSpring2016Lecture511thApril2016.pdf # CSE15LSpring2016Lecture613thApril2016.pdf # CSE15LSpring2016Lecture718thApril2016.pdf # CSE15LSpring2016Lecture820thApril2016.pdf # CSE15LSpring2016Lecture925thApril2016.pdf # # ./Dec: # CSE15LFall2015Lecture192ndDec2015.pdf # # ./Feb: # CSE15LWinter2016Lecture108thFeb2016.pdf # CSE15LWinter2016Lecture1110thFeb2016.pdf # CSE15LWinter2016Lecture1222ndFeb2016.pdf # CSE15LWinter2016Lecture1324thFeb2016.pdf # CSE15LWinter2016Lecture1429thFeb2016.pdf # CSE15LWinter2016Lecture81stFeb2016.pdf # CSE15LWinter2016Lecture93rdFeb2016.pdf # # ./Jan: # CSE15LWinter2016Lecture14thJan2016.pdf # CSE15LWinter2016Lecture26thJan2016.pdf # CSE15LWinter2016Lecture311thJan2016.pdf # CSE15LWinter2016Lecture413thJan2016.pdf # CSE15LWinter2016Lecture520thJan2016.pdf # CSE15LWinter2016Lecture625thJan2016.pdf # CSE15LWinter2016Lecture727thJan2016.pdf # # ./Jun: # CSE15LSpring2016Lecture181stJune2016.pdf # # ./Mar: # CSE15LSpring2016Lecture128thMarch2016.pdf # CSE15LSpring2016Lecture230thMarch2016.pdf # CSE15LWinter2016Lecture152ndMar2016.pdf # CSE15LWinter2016Lecture167thMar2016.pdf # # ./May: # CSE15LSpring2016Lecture112ndMay2016.pdf # CSE15LSpring2016Lecture124thMay2016.pdf # CSE15LSpring2016Lecture139thMay2016.pdf # CSE15LSpring2016Lecture1416thMay2016.pdf # CSE15LSpring2016Lecture1518thMay2016.pdf # CSE15LSpring2016Lecture1623rdMay2016.pdf # CSE15LSpring2016Lecture1725thMay2016.pdf # # ./Nov: # CSE15LFall2015Lecture112ndNov2015.pdf # CSE15LFall2015Lecture124thNov2015.pdf # CSE15LFall2015Lecture139thNov2015.pdf # CSE15LFall2015Lecture1416thNov2015.pdf # CSE15LFall2015Lecture1518thNov2015.pdf # CSE15LFall2015Lecture1623rdNov2015.pdf # CSE15LFall2015Lecture1725thNov2015.pdf # CSE15LFall2015Lecture1830thNov2015.pdf # # ./Oct: # CSE15LFall2015Lecture1028thOct2015.pdf # CSE15LFall2015Lecture35thOct2015.pdf # CSE15LFall2015Lecture47thOct2015.pdf # CSE15LFall2015Lecture512thOct2015.pdf # CSE15LFall2015Lecture614thOct2015.pdf # CSE15LFall2015Lecture719thOct2015.pdf # CSE15LFall2015Lecture821stOct2015.pdf # CSE15LFall2015Lecture926thOct2015.pdf # # ./Sep: # CSE15LFall2015Lecture128thSep2015.pdf # CSE15LFall2015Lecture230thSep2015.pdf # # Doing this will likely produce a bunch of directories named after months, to # clean them up before running a test, you can simply type `make clean` # # # Useful Tips: # ------------ # # 1. The for...in loop: # # If you used the C-style for loop to draw the chessboard in the last # assignment, you will find that the same technique does not apply very # easily when you wish to iterate through a list of files, or an array. The # for...in loop comes in very handy for that purpose. Uncomment the block # below and see how it works # # for name in {Mal,Zoe,Wash,Jayne,Kaylee,Inara,Simon,River,Book} # do # echo $name # done # # 2. find # # As learned in Lab 2, the find command locates all files matched with given # criteria. You may find it useful when you want to filter all slides # associated with a certain month. # # 3. test and conditions # # Conditions in shell scripting may appear unpredictable at first, but once # you get it, it's quite simple. The structure looks like this: # # if condition-cmd; then # if-branch-cmd # else # else-branch-cmd # fi # # where `if` will check the condition-cmd's exit code. If the exit code is 0, # then the script will take the if branch; otherwise it will take the else # branch. # # Conditions in shell script is often used with `test`, which based on its command # line inputs, either exits with 0 when condition is true, or 1 when condition is # false (kind of counter-intuitive, but that? how shell scripting works). # # Spend some time reading the man page for test, and play with it for some # time, and see which configurations are applicable to the task that you need # to do for this assignment. Because test is so often used with if, square # brackets are often used in place of test. For example: # # if [ 1 -lt 2 ]; then # echo TRUE # fi # # will be equivalenet to # # if test 1 -lt 2; then # echo TRUE # fi # # Beware that when you are using the square brackets notation, there needs to # be spaces between the brackets and test's arguments. # # # TIME TO SLAY THE DRAGON! # ------------------------
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
