Write a program in C or a script in bash, called “compare” that takes two numbers on the command line and compares them. The program should print the result of the comparison. Specifically, it should print “<x> is <comparison> <y>”, where <x> is the first number, <y> is the second number and <comparison> is one of “equal to”, “greater than” or “less than”. If the two numbers are equal, the program should have an exit status of zero. The exit status should be non-zero in all other cases. Use pseudo-code if necessary, but try to use valid C or bash syntax wherever possible. You may write the program and test it on UNIX. Write or copy/paste your code here.
C Program |
#include<stdio.h> int main(int numargs, char **argv) { // check if the number of command line arguments is 3 or not if(numargs == 3) { // convert the arguments to integers int x = atoi(argv[1]); int y = atoi(argv[2]); // compare the numbers if( x > y ) printf("%d is greater than %d\n", x, y ); else if(y > x) printf("%d is less than %d\n", x, y ); // else they are equal else { printf("%d is equal to %d\n", x, y ); return 0; } } // outer else else printf("Invalid number of arguments supplied!"); return 1; } |
OUTPUT
Write a program in C or a script in bash, called “compare” that takes two numbers on the command line and compares them....
Unix classes Write a bash script that take in three integer numbers from the command line and print out the sum and the average. Name the script awesum.sh hb1170uxb4:~$ ./awesum.sh 100 200 300 sum: 600 average: 200 hb1170uxb4: ~S
Write a bash script question.sh that accepts one command line argument which is supposed to be a positive integer n. The script should print all odd integers from 1 through n. Write a C or C++ program question.c(pp) to read from stdin as many integers as there are available and then print the squares of all these integers. You should test your code by “./question.sh <n> | ./question” assuming the compiled C/C++ program is question. See the following for a...
Write a bash shell script to print the number of command line arguments and list of command line arguments 3. 12]
Write a Bash script called hello that uses command line arguments to allow the user to put two strings after the command name, when the script is being executed. These strings should represent a first and last name. The script should then write out a greeting to the user that includes the first and last name. Here is an example of how the script might work (the first line represents what the user types to launch the script): [user@HAL] hello...
Write a Bash script called move that could replace the UNIX command mv. 'move' tries to rename the source file (using the UNIX command mv), but if the destination file exists, appends an index number, a sort of version number, to the destination file. So if the user types: move a.txt b.txt and b.txt already exists, move will rename the file to b.txt.1. If b.txt.1 already exists, move must rename the file to be b.txt.2, and so on, until the...
You must write a C program that prompts the user for two numbers (no command line input) and multiplies them together using “a la russe” multiplication. The program must display your banner logo as part of a prompt to the user. The valid range of values is 0 to 6000. You may assume that the user will always enter numerical decimal format values. Your program should check this numerical range (including checking for negative numbers) and reprompt the user for...
Write a Unix Shell Script to print all Armstrong Numbers between 1 and 1000. Your Program should include following key points: Program should have Header Comments. Program should print all Armstrong numbers between 1 and 1000: 1 153 370 371 407
This assignment will assess your ability to write bash scripts. Drop down to the root user using the command sudo su -. Write a bash script named /root/exam02.sh that does the following: Takes exactly 1 argument. If the number of arguments is not 1: Print "Exiting!" and exit with return code 40. If the number of arguments is exactly 1: Print the first argument as per the example below. Example output for your reference: The script should have the following...
Write a C++ program that takes two numbers from the command line and perform and arithmetic operations with them. Additionally your program must be able to take three command line arguments where if the last argument is 'a' an addition is performed, and if 's' then subtraction is performed with the first two arguments. Do not use 'cin' or gets() type functions. Do not for user input. All input must be specified on the command line separated by blank spaces...
Your assignment: Write a program that takes a number from the command line in the program C. It should print: The number. The number of bits in the number that are set to 0 and 1.