Question

In: Computer Science

Write a bash script to find all the files ending with .c recursively for every directory...

Write a bash script to find all the files ending with .c recursively for every directory in your current working directory, then copy each one to a folder called programs, need handle duplicates by appending the number to the end of the file (ex main.c main-1.c ) compile them and generate a report

report should look like:

main.c compiles

prog2.c failed

main-2.c compiles

etc.

Solutions

Expert Solution

#!/bin/bash

#Defining/Creating Program Directory

Programs="./programs"

if [ ! -d $Programs ]; then

   mkdir $Programs

fi

for srcpath in $(find . -type f)

   # if it is reading from this program directory, then don't process it

   do srcdirname=$(dirname $srcpath)

   if [[ "$srcdirname" == "$Programs" ]]; then

       continue

   fi

   # check for file name. If it is not c, then don't process it

   fileext="${srcpath##*.}"

   if [[ $fileext == "c" ]]; then

       #Check the src filename

       dstFile=$(basename $srcpath)

       dstPath="$Programs/$dstFile"

       ((count=0))

       #If the file is already present in dest. path then rename file and retry

       while [[ -f $dstPath ]]

           do ((count++))

           dstPath="$Programs/$(basename $srcpath .c)-$count.c"

       done

       #Copy the source file to final dest file

       cp $srcpath $dstPath

       #This line will help you to see what all files are copied from which directory to programs

       #Uncomment the below and check by yourself after the end of the script

       #Remove this line from final code.

       #echo "cp $srcpath $dstPath" >> filelist.sh

       #gcc will return 0 on success. Redirect all error and output to null

       gcc $dstPath > /dev/null 2>&1

       #Check for return value of gcc compilation

       if [ $? -eq 0 ]

       then echo "$(basename $dstPath)   compiles"

       else

           echo "$(basename $dstPath) failed";

       fi

   fi

done

#Remove a.out generated in compilation process

if [ -f "a.out" ]; then

rm a.out

#If you want to remove programs folder also, uncomment the below line

#rm -rf $Programs

fi


Related Solutions

Write a bash script file that tests each file entry in the current directory. It should...
Write a bash script file that tests each file entry in the current directory. It should determine if it is a file or directory. If it is a file, it will determine if it is readable or not. If it is readable, it will display only the first 4 lines to the terminal in sorted order (just sort the first 4 lines). If it is a directory, it should display the contents of that directory (the files and subdirectories). NOTE:...
this is bash scripting. a. Write a shell script that adds an extension “.deb” to all...
this is bash scripting. a. Write a shell script that adds an extension “.deb” to all the files in a directory. b. Write a command sequence or a script to insert a line “GHIJKLM” at every 10th line of a file? c. Write a bash command or script to find all the files modified in less than 5 days and print the record count of each.
Write a script in C that will do the following : 1. Create the directory ZHW3....
Write a script in C that will do the following : 1. Create the directory ZHW3. 2. Verify that the directory is created by display all information related to the directory and not just the name of the directory. 3. Assume that the input from the command is used to validate password strength. Here are a few assumptions for the password string. • Length – minimum of 8 characters. • Contain alphabets , numbers , and @ # $ %...
Write a bash script to download two files, say file1.gz and file2.gz from a given web...
Write a bash script to download two files, say file1.gz and file2.gz from a given web address to the directory "assignment5"
UNIX/LINUX SCRIPT: Create a named directory and verify that the directory is there by listing all...
UNIX/LINUX SCRIPT: Create a named directory and verify that the directory is there by listing all its contents. Write a shell script to validate password strength. Here are a few assumptions for the password string.   Length – a minimum of 8 characters. • Contain alphabets , numbers , and @ # $ % & * symbols. • Include both the small and capital case letters. give a prompt of Y or N to try another password and displays an error...
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
Write a script that prompts the user for a pathname of a file or directory and...
Write a script that prompts the user for a pathname of a file or directory and then responds with a description of the item as a file along with what the user's permissions are with respect to it (read, write, execute).
Fully Functional Script written in BASH In this section, you will write a fully functional script...
Fully Functional Script written in BASH In this section, you will write a fully functional script to help your manager with an important project that will build upon what you learned from your script work in Milestone Four. After you have written the script, you will execute it to generate a report for all three users (once for Bob, once for Henry, and once for Frank). You should have three unique generated reports in your ~/scripts directory (or more if...
What is a working set? What is the first line of every bash script? What is...
What is a working set? What is the first line of every bash script? What is the permission string (RWXRWXRWX format) of the number 311 What is the base directory of a file system called on windows, and on linux What does the term 'super user' mean in terms of operating systems
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT