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

Write a bash script (exclusively using a single use of the utility ls and bash s

ID: 3825364 • Letter: W

Question

Write a bash script (exclusively using a single use of the utility ls and bash script constructs; no awk, sed, Perl, Python, etc.) that does the following when executed. a) It prints the names of all regular files (that is, not directories) that are readable, writable, and executable by the user in the current directory, one file name per line. It does not explore any subdirectories. Nothing other than the file name should be printed in these lines. b) After printing all the file names, the script prints the number of files printed in the earlier steps with proper annotation as given in the example that appears later. If you use additional Linux commands or a different language, you will not receive any credit for this problem. Suppose the directory appears as below. -rw-r--r-- 1 cfs264sp170225 cfs264sp1702 21 2017-04-16 d1 drwxr-xr-x 2 cfs264sp170225 cfs264sp1702 4096 2017-04-16 dir1 -rw-r--r-- 1 cfs264sp 170225 cfs264sp1702 181 2017-04-16 lsfile -rwxr-xr-x 1 cfs264sp170225 cfs264sp1702 179 2017-04-16 p1 -rwxr-xr-x 1 cfs264sp170225 cfs264sp1702 14 2017-04-16 p2 -r-r-xr-x 1 cfs264spi 70225 cfs264sp1702 828 2017-04-16 signindata -rw-r--r-- 1 cfs264sp 170225 cfs264sp1702 210 2017-04-16 x The following would be the effect of the execution. Note that the file, when given adequate permission should be executable as below. $/p1| p1 p2 Number of files 2 The only Linux command you are allowed to use in the bash script is ls with no options. All other functionality must be accomplished using bash constructs. The command (if stored in a file named p1) should work when invoked as $./p1

Explanation / Answer

shell script code:

#!/bin/bash

count=0
# Looping through all files in current directory
for file in $(ls)
do
   # Checking if file is a regular file and readable, writable and executable by the user
   if [[ -f $file && -r $file && -w $file && -x $file ]]
   then
       echo $file
       ((count++))
   fi
done
echo "Number of files $count"

Output:

186590cb0725:Test bonkv$ ls -tlrh
total 8
-rw-r--r-- 1 bonkv 1896053708 0B Apr 25 18:54 e
-rw-r--r-- 1 bonkv 1896053708 0B Apr 25 18:54 d
-rw-r--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
drwxr-xr-x 2 bonkv 1896053708 68B Apr 25 19:04 abc
-rwxr-xr-x 1 bonkv 1896053708 306B Apr 25 19:07 myfiles.sh

186590cb0725:Test bonkv$ ./myfiles.sh
a
b
myfiles.sh
Number of files 3
186590cb0725:Test bonkv$

After giving execute permission to the file it is able to print that file as well. Below is the example run and Output

186590cb0725:Test bonkv$ chmod u+x e
186590cb0725:Test bonkv$ ls -tlrh
total 8
-rwxr--r-- 1 bonkv 1896053708 0B Apr 25 18:54 e
-rw-r--r-- 1 bonkv 1896053708 0B Apr 25 18:54 d
-rw-r--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
drwxr-xr-x 2 bonkv 1896053708 68B Apr 25 19:04 abc
-rwxr-xr-x 1 bonkv 1896053708 306B Apr 25 19:07 myfiles.sh
186590cb0725:Test bonkv$ ./myfiles.sh
a
b
e
myfiles.sh
Number of files 4
186590cb0725:Test bonkv$

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote