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:...
write a bash shell script using the for-loop construct that counts the number of files and...
write a bash shell script using the for-loop construct that counts the number of files and directories looping through the files in the directory name provided by the user on a prompt by the script and prints "TOO many files" is the count of files is larger than a random number it generates between 20 and 40
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.
In this assignment, you are required to write a Bash script, call it assignment2.sh. Your Bash...
In this assignment, you are required to write a Bash script, call it assignment2.sh. Your Bash script has to accept at least four input arguments, and must: 1) Print to the user a set of instructions explaining how the PATH variable can be used in Bash. 2) Save the manual of the 'awk' command in the file /tmp/help.txt. 3) Shut down your Ubuntu box at 2 o'clock tonight. 4) Store your name and your partner's name in a text file...
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).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT