Question

In: Computer Science

write a bash shell program named L1 as follows . L1 expects exactly 2 command line...

write a bash shell program named L1 as follows . L1 expects exactly 2 command line arguments. expects $1 to contain only digits. expects $2 to have exactly 4 characters (can be any character).

if NOT exactly 2 command line arguments, echo "need 2 args " and exit.

otherwise, if $1 and/or $2 have incorrect value(s) (as above), echo "bad argX", where X is the first incorrect argument, and exit.

otherwise, echo "good ". you can use echo,grep, pipe to check if the argument have the required characters.

example:

>L1 849474 0-=~

good

>L1 47347 8x

bad arg2

>L1 123abc 8d

bad arg1

Solutions

Expert Solution

#!/bin/bash

#Creating a regular expression, which will accept any types of number
re='^[+-]?[0-9]+([.][0-9]+)?$'


#Finding the length of the string using expr
len=`expr length "$2"`

if [[ $# -ne 2 ]]; then
        echo "need 2 args"
elif ! [[ $1 =~ $re ]] ; then
   echo "bad arg1";
elif [[ $len -ne 4 ]]; then
        echo "bad arg2";
else
        echo "good"
fi

Figure 1: Output

Figure 2: Code Screenshot


Related Solutions

Write a shell program named HELLO2(this should be done in linux, using bash) Consider files in...
Write a shell program named HELLO2(this should be done in linux, using bash) Consider files in the current directory whose names end in 't' or 'z'. For such files only, your program should list certain LINES of the file(s). These lines are those that have at least 4 x's somewhere in the line. Note that the x's may be upper- or lower-case, and may be separated by other characters; so the following 3 lines DO match: XXxX and more things...
Write a script named countmatches that expects at least two arguments on the command line. The...
Write a script named countmatches that expects at least two arguments on the command line. The first argument is the pathname of a dna file containing a valid DNA string with no newline characters or white space characters of any kind within it. (It will be terminated with a newline character.) This dna file contains nothing but a sequence of the bases a, c, g, and t in any order. The remaining arguments are strings containing only the bases a,...
Here are the tasks for this assignment: 1. Write a bash command line to determine if...
Here are the tasks for this assignment: 1. Write a bash command line to determine if the server 'syccuxfs01.pcc.edu' is reachable from the syccuxas01.pcc.edu server. 2. As you have read, TCP/IP messages are passed from one device to another until the message reaches its destination. Write a bash command line that will verify that no more than two (2) network devices are used to pass messages from the syccuxas01.pcc.edu server to the www.pcc.edu server. 3. Write a bash command line...
Write a bash shell script that takes exactly one argument, a file name. If the number...
Write a bash shell script that takes exactly one argument, a file name. If the number of arguments is more or less than one, print a usage message. If the argument is not a file name, print another message. For the given file, print on the screen its content.
UNIX ONLY Write a bash script that will accept a filename as a command line argument....
UNIX ONLY Write a bash script that will accept a filename as a command line argument. Your script should first check whether the filename has been passed as argument or not (hint: at least one argument has to be provided). If no argument has been provided your script should display appropriate message and exit. If a file name has been provided, your script should check if the file exists in the current directory and display the contents of the file....
LINUX In Linux command line write a shell script ex1.sh that uses IF THEN to Prompt...
LINUX In Linux command line write a shell script ex1.sh that uses IF THEN to Prompt the user to "Enter a number between 1 and 10". (Hint: Use the 'echo' and 'read' commands in the script. See the slide about the 'read' command) If the number is less than 5, print "The number is less than 5" (Hint: You will read input into a variable; e.g. read NUM. In the IF statement, enclose $NUM in quotes; e.g. "$NUM". Also, remember...
Introduction Write in C++ at the Linux command line a program that is the same as...
Introduction Write in C++ at the Linux command line a program that is the same as the previous collection app project but now uses a class to store the items and also can save the items to a file that can be read back into the array by the user when the program is re-started. You can use your project 1 submission as a starting point or you can do something new as long as it meets the listed requirements....
Write a program that takes two command line arguments at the time the program is executed....
Write a program that takes two command line arguments at the time the program is executed. You may assume the user enters only decimal numeric characters. The input must be fully qualified, and the user should be notified of any value out of range for a 23-bit unsigned integer. The first argument is to be considered a data field. This data field is to be is operated upon by a mask defined by the second argument. The program should display...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT