Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Now that you have the Korn Shell (ksh) installed and have learned more about the

ID: 3827765 • Letter: N

Question

Now that you have the Korn Shell (ksh) installed and have learned more about the language itself, it is time to do some programming:

Write a Korn shell script (and show the code for it here) that accepts exactly 1 command line argument that must be a positive integer. The script will print a comma separated list of integers, all on the same line, starting with the initial command line value and decreasing it by one-to-one. The last printed value must not be followed by a comma. Do not write this script to be executed interactively.

The script must be to handle the following error situations:

Incorrect number of arguments and

Non-positive arguments.

The script file name must be: printnum.sh

The script permissions must be 705 (show in your Word document).

Sample Output (provide yours in same Word document)

Sunny Day Scenarios:

csis345@csis345-vm:~$ sh printnum.sh 3

3, 2, 1

csis345@csis345-vm:~$

csis345@csis345-vm:~$ sh printnum.sh 10

10, 9, 8, 7, 6, 5, 4, 3, 2, 1

csis345@csis345-vm:~$

Rainy Day Scenarios:

csis345@csis345-vm:~$ sh printnum.sh

error: program must be executed with 1 argument. usage: printnum.sh value (where value >= 1)

csis345@csis345-vm:~$ sh printnum.sh 2 5

error: program must be executed with 1 argument. usage: printnum.sh value (where value >= 1)

csis345@csis345-vm:~$ sh printnum.sh -1

error: argument must be a positive number. usage: printnum.sh value (where value >= 1)

Add at least 5 more scenarios in your Word document.

Write a Korn shell script (and show the code for it here) that will determine which file in a directory has the maximum number of lines (this may be different than the file that has the maximum number of bytes). After determining the file with the maximum number of lines, the script will print out the name of the file and the number of lines. The script must only focus on files and ignore subdirectories. The command wc may be helpful. Do not write this script to be executed interactively.

The script must allow either no arguments or 1 argument.

If zero arguments are specified, the script by default will examine the files in the current directory.

If 1 argument is specified, the argument must be the name of a directory. The script will then examine the files in the specified directory.

The script must be able to handle the following error conditions:

More than 1 argument is specified.

The specified argument is not a directory.

The script file name must be: maxlines.sh

The script permissions should be 705

Sample Output (provide yours in same Word document)

Sunny Day Scenarios (Note: output depends on current directory and may not match the values below):

csis345@csis345-vm:~$ sh maxlines.sh

File maxlines.sh has maximum number lines (36 lines).

csis345@csis345-vm:~$ sh maxlines.sh /etc

File mime.types has the maximum lines with 827 lines.

Rainy Day Scenarios:

csis345@csis345-vm:~$ sh maxlines.sh junk

error: argument must be a directory usage: maxlines.sh [directory]

csis345@csis345-vm:~$ sh maxlines.sh junk trash

error: can only use 0 or 1 arguments. usage: maxlines.sh [directory]

Add at least 5 more scenarios in your Word document.

·      Submit a zip file with all Korn Shell script files for parts a and b.

Explanation / Answer

#!/bin/ksh

# Name of the script: printnum.sh
# Program to print comma separated list of integers, all on the same line, starting with the initial command line value and decreasing it by one-to-one

# checking if command line arguments is one argument
if [[ $# -eq 1 ]]
then
   # checking if one argument passed is >= 1
   if [[ $1 -ge 1 ]]
   then
       num=$1
   else
       print "error: argument must be a positive number. usage: $0 value (where value >= 1)"
       exit
   fi
else
   print "error: program must be executed with 1 argument. usage: $0 (where value >= 1)"
   exit
fi

while [[ $num -gt 0 ]]
do
   if [[ $num -eq 1 ]]
   then
       print -n "$num"
   else
       print -n "$num,"
   fi
   num=`expr $num - 1`
done
print " "


output:
printnum.sh has 705 permisssion
186590cb0725:Test bonkv$ ls -tlrh printnum.sh
-rwx---r-x 1 bonkv 1896053708 698B Apr 28 07:10 printnum.sh

186590cb0725:Test bonkv$ ./printnum.sh
error: program must be executed with 1 argument. usage: ./printnum.sh (where value >= 1)
186590cb0725:Test bonkv$ ./printnum.sh 1 2
error: program must be executed with 1 argument. usage: ./printnum.sh (where value >= 1)
186590cb0725:Test bonkv$ ./printnum.sh 2 3 4
error: program must be executed with 1 argument. usage: ./printnum.sh (where value >= 1)
186590cb0725:Test bonkv$ ./printnum.sh 0
error: argument must be a positive number. usage: ./printnum.sh value (where value >= 1)
186590cb0725:Test bonkv$ ./printnum.sh 0.5
error: argument must be a positive number. usage: ./printnum.sh value (where value >= 1)


186590cb0725:Test bonkv$ ./printnum.sh 10
10,9,8,7,6,5,4,3,2,1

186590cb0725:Test bonkv$ ./printnum.sh 3
3,2,1

186590cb0725:Test bonkv$ ./printnum.sh 20
20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1


------------------------------


#!/bin/ksh

# script name: maxlines.sh
# script to determine which file in a directory has the maximum number of lines

if [[ $# -eq 1 ]]
then
   dir=$1
# if no argument is passed current directory will be the directory to process
elif [[ $# -eq 0 ]]
then
   dir=`pwd`
else
   print "error: can only use 0 or 1 arguments. usage: $0 [directory]"
   exit
fi

# checking if the argument obtained is a directory or not
if [[ ! -d "$dir" ]]
then
   print "error: argument must be a directory usage: $0 [directory]"
   exit
fi

maxlines=0
for file in `ls $dir`
do
   # skipping the directory files
   if [[ ! -f "$dir/$file" ]]
   then
       continue
   fi

   num_lines=`wc -l $dir/$file|awk '{print $1}'`
   if [[ $num_lines -gt $maxlines ]]
   then
       maxlines=$num_lines
       maxfile=$file
   fi
done

print "File $maxfile has the maximum lines with $maxlines lines"


output:

script has 705 permisssions:
186590cb0725:Test bonkv$ ls -tlrh maxlines.sh
-rwx---r-x 1 bonkv 1896053708 830B Apr 28 07:56 maxlines.sh

Here is the list of files in my directory:
186590cb0725:Test bonkv$ ls -tlrh
total 64
-rwxr--r-- 1 bonkv 1896053708 0B Apr 25 18:54 e
-rwxr--r-- 1 bonkv 1896053708 0B Apr 25 18:54 d
-rwxr--r-- 1 bonkv 1896053708 0B Apr 25 18:54 c
-rwxr--r-- 1 bonkv 1896053708 0B Apr 25 18:54 b
-rwxr--r-- 1 bonkv 1896053708 0B Apr 25 18:54 a
-rwxr-xr-x 1 bonkv 1896053708 306B Apr 25 19:07 myfiles.sh
-rw-r--r-- 1 bonkv 1896053708 1.3K Apr 26 06:15 testing.sh
-rw-r--r-- 1 bonkv 1896053708 68B Apr 26 06:48 testfile
-rwxr-xr-x 1 bonkv 1896053708 1.5K Apr 26 07:02 test.pl
-rwxr-xr-x 1 bonkv 1896053708 654B Apr 26 12:00 tps.pl
-rw-r--r-- 1 bonkv 1896053708 108B Apr 27 06:11 test.py
-rwx---r-x 1 bonkv 1896053708 698B Apr 28 07:10 printnum.sh
drwxr-xr-x 4 bonkv 1896053708 136B Apr 28 07:31 abc
drwxr-xr-x 4 bonkv 1896053708 136B Apr 28 07:34 directory
drwxr-xr-x 4 bonkv 1896053708 136B Apr 28 07:47 testdir
-rwx---r-x 1 bonkv 1896053708 830B Apr 28 07:56 maxlines.sh


186590cb0725:Test bonkv$ ./maxlines.sh abc directory
error: can only use 0 or 1 arguments. usage: ./maxlines.sh [directory]
186590cb0725:Test bonkv$ ./maxlines.sh e
error: argument must be a directory usage: ./maxlines.sh [directory]
186590cb0725:Test bonkv$ ./maxlines.sh ~ ~/Desktop
error: can only use 0 or 1 arguments. usage: ./maxlines.sh [directory]
186590cb0725:Test bonkv$ ./maxlines.sh ~ . ~/Desktop ~/Downloads
error: can only use 0 or 1 arguments. usage: ./maxlines.sh [directory]

186590cb0725:Test bonkv$ ./maxlines.sh
File test.pl has the maximum lines with 66 lines
186590cb0725:Test bonkv$ ./maxlines.sh directory
File a has the maximum lines with 2 lines
186590cb0725:Test bonkv$ ./maxlines.sh abc
File 1 has the maximum lines with 6 lines
186590cb0725:Test bonkv$ ./maxlines.sh directory/
File a has the maximum lines with 2 lines
186590cb0725:Test bonkv$ ./maxlines.sh ~/Desktop
File Projections_screenshot.png has the maximum lines with 1794 lines

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote