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...
I NEED THIS CODE FOR C++ USING MONITORS PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include...
I NEED THIS CODE FOR C++ USING MONITORS PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #define THREADS 10 // Number of Thread //bridge declared with array of character and integer value void Bridge(char array[], int value); // Global Variable int North = 1; //For North Number int South = 1; //For South Number pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; // Setting Up MUTEX for lock //Thread for North farmer void NorthFarmer(){ pthread_mutex_lock(&mutex1); char array[5] = "North"; // North printf("%s Tunbridge...
Please write code in C++ and include all header files used not bits/stdc: (Same number subsequence)...
Please write code in C++ and include all header files used not bits/stdc: (Same number subsequence) Write an O(n) program that prompts the user to enter a sequence of integers ending with 0 and finds longest subsequence with the same number. Sample Run Enter a series of numbers ending with 0: 2 4 4 8 8 8 8 2 4 4 0 The longest same number sequence starts at index 3 with 4 values of 8
C CODE PLZ! All instructions for what to do in code #include <stdio.h> int main(int argc,...
C CODE PLZ! All instructions for what to do in code #include <stdio.h> int main(int argc, char **argv) { int n, k, l, r, t, d, i; char str[65]; /* Make the user enter a non-negative integer */ printf("Please enter a non-negative integer: "); scanf("%d", &n); while (n < 0) { printf("Sorry, your input is incorrect.\n"); printf("Please enter a non-negative integer: "); scanf("%d", &n); } /* Convert the integer to reversed binary: e.g. 6 gets converted to 011 */ if...
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...
Create shell scripts, .sh file. Please include a shebang line at the top of the script...
Create shell scripts, .sh file. Please include a shebang line at the top of the script as well as appropriate comments through out. Write a script that plays a simple “guess the number” game with the user. It should select a random integer in the range [0 - 10], ask the user to guess the value, and provide feedback of the form “too high”, “too low”, or “congratulations” as appropriate. After the correct value is guessed, the script should terminate.
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...
Write bash shell scripts for the following problems and submit your answers a single. A )...
Write bash shell scripts for the following problems and submit your answers a single. A ) Write a linux bash shell script that reads your first and lastname as arguments and print them as shown below. In my example, I name the script as printname.sh. In the example, the two arguments follow the shell scriptname. Submit the script and output print screen. # sh ./printname.sh Abdullah Konak My first name is Abdullah My surname is Konak B ) Write a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT