Question

In: Computer Science

Hello, I am using BASH. I need to write a conditional statement using grep. I want...

Hello, I am using BASH. I need to write a conditional statement using grep. I want the computer to echo true if it detects any numerical character in the first line of StrepList.txt. However, the computer tells me that "grep: [0-9]: No such file or directory"

if (head -n 1 StrepList.txt | grep -o [0-9] -eq TRUE);then

echo "Contains numbers"

else

echo "No numbers"

fi

Solutions

Expert Solution

You've made some syntactical mistakes.

There is a mistake in the if statement,i.e,

if (head -n 1 StrepList.txt | grep -o [0-9] -eq TRUE) , the inner command must be enclosed in brackets preceded by $ symbol.

$(head -n 1 StrepList.txt | grep -o [0-9]) means it represents the whole output.

Another mistake is that the command returns a numerical value not a boolean value. Therefore "-eq TRUE" is not a correct statement. And there is no need of "semi column(;)" at the end of the if statement.

And use '-c' in the grep command instead of '-o'.

And use these brackets [ ] for if conditions.

Therefore the corrected code would be:

if [ $(head -n 1 StrepList.txt | grep -c [0-9]) -eq 1 ]
then
echo "Contains numbers"
else
echo "No numbers"
fi

expected output:

if first line of file contains any number then

if first line doesn't have any numbers then,


Related Solutions

I am attempting to write a script using bash on Linux. My program must save the...
I am attempting to write a script using bash on Linux. My program must save the long list format of the parent directory to a text file. Then, it must print how many items are in this parent directory. Then, it must sort the file in reverse order and save it to a different text file. I understand that the parent directory is accessed using cd .., and I understand that a file can be written to by echoing and...
this is bash scripting. a. Explain the difference between grep and egrep? b. Write a command...
this is bash scripting. a. Explain the difference between grep and egrep? b. Write a command to list files where third letter is x or y? c. Write command to remove array element with id 5
Hello, I need to convert this java array into an array list as I am having...
Hello, I need to convert this java array into an array list as I am having trouble please. import java.util.Random; import java.util.Scanner; public class TestCode { public static void main(String[] args) { String choice = "Yes"; Random random = new Random(); Scanner scanner = new Scanner(System.in); int[] data = new int[1000]; int count = 0; while (!choice.equals("No")) { int randomInt = 2 * (random.nextInt(5) + 1); System.out.println(randomInt); data[count++] = randomInt; System.out.print("Want another random number (Yes / No)? "); choice =...
Using If-Else conditional statement write a Visual Basic Program to display: (i) “STOP” if the user...
Using If-Else conditional statement write a Visual Basic Program to display: (i) “STOP” if the user enters “R” (ii) “CAUTION” if the user enters “Y” (iii) “GO” if the user enters “G” (iv) “Invalid” if the user enters any other character
hello, I am writing an article why pharmacists need to be utilized as precribers in the...
hello, I am writing an article why pharmacists need to be utilized as precribers in the U.S in effort to help accessibility for women to obtain birth control? any thoughts based on evidence? please provide reference of articles.
Hello, I am in need of some assistance in interpreting the data for the two variables...
Hello, I am in need of some assistance in interpreting the data for the two variables I did in a t-test for in Excel. Variable 1 is Relationship with Direct Supervisor and Variable 2 is the Workplace Happiness Rating. I am supposed to write a 125- to 175-word summary of my interpretation of the results of the t test. t-Test: Two-Sample Assuming Equal Variances Variable 1 Variable 2 Mean 2.5 7.4 Variance 1.030612245 2 Observations 50 50 Pooled Variance 1.515306122...
Hello, I am writing the initial algorithm and refined algorithm for a program. I just need...
Hello, I am writing the initial algorithm and refined algorithm for a program. I just need to make sure I did it correctly. I'm not sure if my formula is correct. I appreciate any help, thank you! TASK: Write a program that will calculate the final balance in an investment account. The user will supply the initial deposit, the annual interest rate, and the number of years invested. Solution Description: Write a program that calculates the final balance in an...
Hello I am needing an example of how to write an assembly (MIPS) code that with...
Hello I am needing an example of how to write an assembly (MIPS) code that with will ask the user for two numbers then for addition or multiplication by typing in + or * into the command prompt. For example if I type in the number 2 and 5 then + The code should add the sum between the two numbers like 2 + 3 + 4 + 5 = 14. If multiplication is implemented then it will do the...
Hello, I am trying to write a C++ program that will do the following: Use the...
Hello, I am trying to write a C++ program that will do the following: Use the STL stack container in a program that reads a string, an arithmetic expression to be exact, one character at a time, and determines if the string has balanced parenthesis – that is, for each left parenthesis there is exactly one matching right parenthesis later in the string.                         Use the following strings to test your program. A+ B - C A * B / (C...
Hello! I do not want you to write an essay for me by any means. I...
Hello! I do not want you to write an essay for me by any means. I am just completely lost, without inspiration or direction. I am hoping for a few ideas to build from. It's for a composition class, so facts and figures are not important. You'll find the prompt below. Thank you! A person’s view of America is substantially influenced by how one got here—by choice or by force—and how well one has integrated into the dominant culture. Immigrants...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT