Question

In: Computer Science

Can you write the shell scripts for these C files (code in C): #include <stdio.h> #include...

Can you write the shell scripts for these C files (code in C):

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main(int argb, char *argv[]){
char ch;
int upper=1;
if(argb>1){
if(strcmp(argv[1],"-s")==0){
upper=1;
}
else if(strcmp(argv[1],"-w")==0){
upper=0;
}
}
while((ch=(char)getchar())!=EOF){
if(upper){
printf("%c",toupper(ch));
}
else{
printf("%c",tolower(ch));
}
}
return 0;

}

========================================================

#include <stdio.h>
#include <stdlib.h>

int calcRange(int array[],int size){
int max=array[1];
int min=array[0];
int a=0;
for(a=1;a<size;a++){
if(array[a]>max){
max=array[a];
}
}
for(a=1;a<size;a++){
if(array[a]<min){
min=array[a];
}
}
printf("%d-%d=%d\n",max,min,max-min);
}

int main(int arga, char **argv){
int i;
if(arga>1){
int array[arga];
for(i=1;i<arga;i++){
array[i]=atoi(argv[i]);
}
int range=calcRange(array,arga);
printf("range=%d\n",range);
}
}

Solutions

Expert Solution

Please find the requested scripts below.

Please provide your feedback
Thanks and Happy learning!

#!/bin/bash
#Above line sets the environment for the script to bash

#Check for the command line argument
if [ $# -eq 1 ]
then
    if [ "$1" == "-s" ]
    then
       upper=1;
    elif [ "$1" == "-w" ]
    then
       upper=0;
    else
        echo "Invalid argument $1. Valid values are -s and -w"
        exit 0
    fi
else
    echo "There should be exactly one command line argument. Valid argument values are -s or -w"
    exit 0;
fi

#Press ctrl D for EOF character
while read ch
do
    if [ $upper -eq 1 ]
    then
        #Convert the character to upper case
        echo `echo "$ch" | tr [a-z] [A-Z]`
    else
        #Convert the character to lower case
        echo `echo "$ch" | tr [A-Z] [a-z]`
    fi
done

#!/bin/bash
#Above line sets the environment for the script to bash

calcRange()
{
        # Shift all arguments to the left (original $1 gets lost)
        shift;
        # Rebuild the array after excluding the first argument(ie array size)
    arr=("$@")
        max=${arr[1]}
        min=${arr[0]}
        
        #find the max element from the array
    for i in "${arr[@]}"
    do
            if [ $i -gt $max ]
                then
                   max=$i
                fi
    done
        
    #find the min element from the array
    for j in "${arr[@]}"
    do
            if [ $j -lt $min   ]
                then
                   min=$j
                fi
    done
    
        range=`expr $max - $min`
    echo "$max-$min=$range"
}

#Check for the command line argument
if [ $# -gt 1 ]
then
    k=0;
    for i in $@
        do
        myArr[k]=$i
        k=`expr $k + 1`
        done
else
    echo "There should be more than one command line argument."
    exit 0;
fi

calcRange "${myArr[@]}"

Related Solutions

Can you translate this C code into MIPS assembly? #include <stdio.h> #include <math.h> #include <stdlib.h> double...
Can you translate this C code into MIPS assembly? #include <stdio.h> #include <math.h> #include <stdlib.h> double fact (double); void main () { int angle_in_D; double term, angle_in_R; float sine = 0; unsigned int i = 1; double sign = 1; int n = 1000; printf ("Please enter an angle (Unit: Degree): "); scanf ("%d", &angle_in_D); angle_in_R = angle_in_D * M_PI / 180.0; do { term = pow(-1,(i-1)) * pow (angle_in_R, (2*i - 1)) / fact (2*i - 1); sine =...
I need the following C code converted to java or C++ #include <stdio.h> #include <stdlib.h> typedef...
I need the following C code converted to java or C++ #include <stdio.h> #include <stdlib.h> typedef struct node { struct node *left; struct node *right; long data; long leftSize; } node; void btreeInsert(node *new, node **rootptr) { node *parent = NULL, *cursor; /* Find parent */ cursor = *rootptr; while (cursor != NULL) { parent = cursor; if (new->data < cursor->data) { cursor->leftSize += 1; cursor = cursor->left; } else { cursor = cursor->right; } } /* Insert node below...
Can you please write a pseudocode for the following: #include <stdio.h> int main(){    printf("Welcome to...
Can you please write a pseudocode for the following: #include <stdio.h> int main(){    printf("Welcome to my Command-Line Calculator (CLC)\n");    printf("Developer: Your name will come here\n");    printf("Version: 1\n");    printf("Date: Development data Will come here\n");    printf("----------------------------------------------------------\n\n");    //choice stores users input    char choice;    //to store numbers    int val1,val2;    //to store operator    char operation;    //flag which leths the loop iterate    //the loop will break once the flag is set to 0...
In this lab, you will learn how to create shell scripts using the Bourne Shell Scripting...
In this lab, you will learn how to create shell scripts using the Bourne Shell Scripting language. The student will have to do research on the Bourne Shell Scripting language and then writ a script that asks the First Name , Last Name, Age, and Country of origin of the student and then print out these items in a statement Then the students will write a 3 to 4-page paper (not including the title and references pages) describing how the...
Code the following in C++ and make sure to include all three files required at the...
Code the following in C++ and make sure to include all three files required at the end. Hotdogs – The World’s Greatest Cost Effective Wholesome Nutritious Food Due to world food supply scarcity and the burgeoning populations of the world’s countries, your hot stand business is globalizing to satisfy the world’s desire for a cost effective wholesome nutritious food source. Market studies have shown that tens of billions of people are craving for the eponymous hotdog which will result in...
Include<stdio.h> In C program Write a program that prompts the user to enter an integer value....
Include<stdio.h> In C program Write a program that prompts the user to enter an integer value. The program should then output a message saying whether the number is positive, negative, or zero.
1. Write a shell code that prints the integers from 80-90 2. Write a shell code...
1. Write a shell code that prints the integers from 80-90 2. Write a shell code that prints the sum of odd integers that are inputted by the user 3. Write a shell code that reads a set of integers {125, 0, 122, 129, 0, 117} and prints {125, 122, 129, 117} Programming Language: Linux
Create shell scripts, each in its own .sh file. Please include a shebang line at the...
Create shell scripts, each in its own .sh file. Please include a shebang line at the top of the script as well as appropriate comments through out. Write a script that reads sunspot data from data-shell/data/sunspot.txt and uses awk to print a list of the total number of sunspots for each year on record as well as the twelve monthly averages across the record. Note: "awk allows you to index arrays using strings, so as you traverse the data file,...
Please Write Code in C++ and include the correct #include <header> and not a catchall such...
Please Write Code in C++ and include the correct #include <header> and not a catchall such as bits/stdc: Write a recursive, string-valued function, replace, that accepts a string and returns a new string consisting of the original string with each blank replaced with an asterisk (*) Replacing the blanks in a string involves: Nothing if the string is empty Otherwise: If the first character is not a blank, simply concatenate it with the result of replacing the rest of the...
Please write shell scripts in Kali Linux to complete the following tasks. Task A - Take...
Please write shell scripts in Kali Linux to complete the following tasks. Task A - Take your UIN Write a script like below that reads and displays the MIDAS ID if the following requirements are satisfied: Only lower case letter [a-z] and numeric character[0-9] are allowed MIDAS ID must between 4 to 8 digits Test your script with the following examples: Your MIDAS ID in lower case Your MIDAS ID w.one upper case A string less than 4 digits A...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT