I need some help with this shell scripts assignment. I have no idea where to sta
ID: 3655027 • Letter: I
Question
I need some help with this shell scripts assignment. I have no idea where to start and I will provide the instructions.
"In this assignment, you will demonstrate your ability to write simple shell scripts. This is also the closest thing to a final exam that this course has, so it will challenge you to pull together a variety of lessons from throughout the course.
Every now and then, I find myself with a large number of files that have inapppriateextensions(the set of characters in the file name after the last '.') that need to be changed. For example, a complicated C++ program, developed by someone on a Windows machine where file names are not case sensitive, might have a number of files ending in ".CPP" and ".H". Not only are these inconsistent with the conventional ".cpp" and ".h" endings, but they can pose a real issue with compiling the code. If the code contains statements like
and the file name is "Utilities.H", the code will not compile on *nix systems, though it might on a Windows system.
In this assignment you will be working towards a script that can be used to fix this and similar problems by giving a desired extension and then a group of files that we wish renamed to use that extension instead of whatever final extension they have at the moement, e.g.
You'll be working on this assignment in 3 stages.
Within that directory, create a shell script,chExt1.shtaking two parameters:
For example, (assuming you have cd'd into yourscriptAsstdirectory,
Hint: Try to get the current name of the file into a shell variable (e.g.,$oldName). Then use a sed command to rewrite that value to remove the file extension, and store the result in a second shell variable (e.g.,$newName). Then append the desired new extension onto that. Finally, issue the actual command to rename the file. There are probably other ways to achieve this effect as well, but all of the info you need for the approach suggested here has been covered in the Lecture Notes.
Within the same directory, create a shell script,chExt2.shtaking the same two parameters, that behaves the same as the first script for files that exists, but for files that do not exist, prints a message
No other messages should be issued, including error messages from commands invoked by your script.
For example,
Give the command:
to complete the assignment."
Any help would be greatly appreciated. Feel free to message me for more details.
Explanation / Answer
Shell script This article is about scripting in UNIX- like systems. For batch programming in DOS, OS/2 and Windows, see Batch file . For batch programming in Windows PowerShell shell, see Windows PowerShell#Scripting . A shell script is a script written for the shell , or command line interpreter , of an operating system. The shell is often considered a simple domain-specific programming language.[1] Typical operations performed by shell scripts include file manipulation, program execution, and printing text. Many shell script interpreters double as command line interface, such as the various Unix shells, Windows PowerShell or the MS-DOS COMMAND.COM. Others, such as AppleScript or the graphical Windows Script Host (WScript.exe), add scripting capability to computing environments without requiring a command line interface. Other examples of programming languages primarily intended for shell scripting include DCL and JCL . Capabilities Shortcuts In its most basic form, a shell script can provide a convenient variation of a system command where special environment settings, command options, or post-processing is applied automatically, but in a way that allows the new script to still act as a fully normal Unix command. One example would be to create a version of ls, the command to list files, giving it a shorter command name of l, which would be normally saved in a user's bin directory as /home/ username/bin/l, and a default set of command options pre-supplied. #!/bin/sh LC_COLLATE=C ls -FCas "$@" Here, the first line (Shebang) indicates which interpreter should be used to execute the rest of the script, and the second line makes a listing with options for file format indicators, columns, all files (none omitted), and a size in blocks. The LC_COLLATE=C sets the default collation order to not fold upper and lower case together, and the "$@" causes any parameters given to l to be passed through as parameters to ls, so that all of the normal options and other syntax known to ls can still be used. The user would then be able to simply use l for the most commonly used short listing. Another example of a shell script that could be used as a shortcut would be to print a list of all the files and directories within a given directory. #!/bin/sh clear ls -l -a In this case, the shell script would start with its normal starting line of #!/ bin/sh . Following this, the script executes the command clear which clears terminal of all text before going to the next line. The following line provides the main function of the script. The ls -l -a command list the files and directories that are in the directory that the script is being run in. The ls command attributes could be changed to reflect the needs of the user. Batch jobs Shell scripts allow several commands that would be entered manually at a command line interface to be executed automatically, and without having to wait for a user to trigger each stage of the sequence. For example, in a directory with three C source code files, rather than manually running the four commands required to build the final program from them, one could instead create a C shell script, here named build and kept in the directory with them, which would compile them automatically: #!/bin/csh echo compiling... cc -c foo.c cc -c bar.c cc -c qux.c cc -o myprog foo.o bar.o qux.o echo done. The script would allow a user to save the file being edited, pause the editor, and then just run ./build to create the updated program, test it, and then return to the editor. Since the 1980s or so, however, scripts of this type have been replaced with utilities like make which are specialized for building programs. Generalization Simple batch jobs are not unusual for isolated tasks, but using shell loops, tests, and variables provides much more flexibility to users. A Bash (Unix shell) script to convert JPEG images to PNG images, where the image names are provided on the command line - possibly via wildcards - instead of each being listed within the script, can be created with this file, typically saved in a file like /home/username/bin/ jpg2png #!/bin/bash for jpg; do # use $jpg in place of each filename given, in turn png="${jpg%.jpg}.png" # find the PNG version of the filename by replacing .jpg with .png echo converting "$jpg" ... # output status info to the user running the script if convert "$jpg" jpg.to.png ; then # use the convert program (common in Linux) to create the PNG in a temp file mv jpg.to.png "$png" # if it worked, rename the temporary PNG image to the correct name else # ...otherwise complain and exit from the script echo 'error: failed output saved in "jpg.to.png".' >&2 exit 1 fi # the end of the "if" test construct done # the end of the "for" loop echo all conversions successful # tell the user the good news exit 0 The jpg2png command can then be run on an entire directory full of JPEG images with just jpg2png *.jpg