Shell Scripting 3ΒΆ

In this lab, we will apply the shell scripting commands from the previous lab to create more complex scripts involving several commands. Since getting used to the syntax in Unix commands is not always straightforward, feel free to refer back to to the lab handouts for the Unix and Shell script labs to revisit the commands, or use the man pages.

  1. Write a shell script that uses the various env variables to print the following to the terminal: user and login names, the current directory, the user’s home directory, the current shell, and the path. Example output:

    $ ./listinfo.csh
    User: egdaub (login: egdaub)
    Current Directory: /gaia/home/egdaub/Documents
    Home Directory: /gaia/home/egdaub
    Current Shell: /bin/tcsh
    Current Path: /usr/bin:/bin:/usr/sbin:...
    
  2. Write a shell script that lists the name of the file and the size of the file for all files in the current directory.

  3. Write a shell script to check if you can execute a file. The script should take the file name as an input argument. The script output should print the file name, followed by one of the following three messages: has execute permissions if the user can execute, does not have execute permissions if the user cannot execute, or does not exist if the file does not exist.

  4. Write a shell script that copies a file, adding the date to the end of the filename of the copied file. The file should be given as a command line input, and the date should be formatted as YYMMDD (i.e. January 10, 2016 is 160110). Append the date just before the file extension. For example, for a file test.txt I would run the script as follows:

    $ ls test*
    test.txt
    $ ./adddate.csh test.txt
    $ ls test*
    test.txt           test161108.txt
    

    You will find the date command useful, and in particular its formatting capabilities (try date +%y%m%d). You may also find the following string operations useful:

    $ set file = file.txt
    $ echo $file:r # prints file
    $ echo $file:e # prints txt
    

    If the input file does not exist, then print an error message to the terminal and do not change any files.

  5. Write a shell script to find all executables within the directories in your PATH that contain a text pattern (which is specified as a command line input). Recall that you can get the PATH formatted as strings separated by colons with $PATH, and as an array using $path.

  6. Write a shell script to copy all files in the current directory that have been modified in the past \({n}\) hours (\({n}\) should be specified as a command line input) to a target directory specified as a command line input. If the target directory does not exist, your script should create it.