Getting Started (Chap 5)

Assignment 4 Answers

  1. Create a script file called nf that contains instructions to count the number of files in the current directory. You can check your answers here.
    ls | wc -l
    
  2. What command do you use to make this file executable?
    chmod +x nf
    
  3. What directory should you put the file in?
    ~/bin
    
  4. Add a comment at the begining of the file with your name and the date
    # Phil Aylesworth, October 2004
    ls | wc -l
    
  5. Add some text to the output to describe the number. i.e. "Files in the current directory:"
    # Phil Aylesworth, October 2004
    echo "Files in the current directory:"
    ls | wc -l
    
  6. Modify the script so that it includes the absolute pathname of the directory. This is stored in the variable called PWD.
    # Phil Aylesworth, October 2004
    echo "Files in $PWD:"
    ls | wc -l
    
  7. Store your first name in a variable and then display it with a meaningful message.
    name=Phil
    echo "My name is $name."
    
  8. Add a comment on the same line as you set the variable.
    name=Phil    # This is my name
    echo "My name is $name."
    
  9. At the command prompt or in a script file, create two variables with numbers stored in them. Calculate the product (multiplication) of these and display it.
    a=54
    b=12
    echo "The product of $a and $b is $((a*b))."
    
  10. Assign the value of this product to another variable then display that new variable.
    a=54
    b=12
    c=$((a*b))
    echo "The product of $a and $b is $c."