Member Avatar for Member #1163902

Write a shell script to create a directory called “page1” and ask the user if he/she wants to copy a file to TESTING directory.
You should find out that the file has read, write and execute permission. If the file has write permission then copy the file to the directory; denied otherwise.

Suppose we have a file called ‘WelcomeHome’ with 100 lines of hotel names, each name on a line. Write a script to print the hotel names from line number 20 to line number 30 inclusive. Store the results in a file called ‘ListNumbers’. Sort the file in alphabetical order.

Reverend Jim commented: Lazy AND rude. -3

Dani AI

Generated

Two compact, practical solutions with brief notes. The first script creates page1, reports whether a chosen file has read/write/execute bits, and (per the assignment) only copies the file to TESTING if it has write permission. The second extracts lines 20–30 from WelcomeHome and stores them sorted in ListNumbers. As noted, posting rules discourage doing others’ homework for them; these examples are minimal and intended to illustrate the required checks and commands.

#!/bin/sh
mkdir -p page1

printf "Copy a file to TESTING directory? (y/n) "
read ans
case "$ans" in
  [Yy]*)
    printf "File to check: "
    read file
    [ ! -e "$file" ] && { echo "File not found: $file"; exit 1; }
    perms=""
    [ -r "$file" ] && perms="${perms}r"
    [ -w "$file" ] && perms="${perms}w"
    [ -x "$file" ] && perms="${perms}x"
    echo "Permissions for '$file': $perms"
    if [ -w "$file" ]; then
      mkdir -p TESTING
      cp -- "$file" TESTING/ && echo "Copied '$file' to TESTING/" || echo "Copy failed."
    else
      echo "Write permission missing: copy denied."
    fi
    ;;
  *)
    echo "No copy requested."
    ;;
esac

Notes on script 1: -r, -w, -x test the current user’s access. Copying typically requires read access to the source and write+execute on the destination directory; the assignment explicitly requires checking write permission on the file, so the script enforces -w. Always quote variables ("$file") to handle spaces; cp -- prevents treating filenames that start with - as options.

#!/bin/sh
[ -f WelcomeHome ] || { echo "WelcomeHome not found"; exit 1; }
sed -n '20,30p' WelcomeHome | sort > ListNumbers

Notes on script 2: sed -n '20,30p' uses 1-based line numbers and will produce fewer lines if the file is shorter than 30 lines. Use wc -l WelcomeHome to check length before extracting. For case-insensitive sorting add sort -f; to remove duplicates add sort -u.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.