Question

Bash Help Please. I need help with taking a txt document with commands and testing to...

Bash Help Please. I need help with taking a txt document with commands and testing to see if they exist in bash. For example [clear], [zip]...ect are in the txt file. At the moment I have

for wordC in $(<commands.txt)
do

if [[ $command_wordC >/dev/null ]]; then

But it always goes to the else statement even if the command should exist. I thought about using Grep but im not entirely sure the purpose of it or how to use it correctly. Could anyone help please? Please explain if you use grep or any commands like that because im new to bash and would like to understand.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

codeToCopy:

#!/bin/bash

#Reading each word from file

for wordC in $(<commands.txt)

do

# There is a built-in command to check whether a command exists or not.

# The 'type' command gives you the location of the script/executable

# of the specified command. If the command does not exist, it throws an error saying that

# command is not found. $wordC > /dev/null only redirects standard output. As we don't

# want to show the errors on the screen, appending(2>&1) the standard errors to standard output.

# 0 - represents standard input

# 1 - represents standard output

# 2 - represents standard error

# You are using '[[ ]]' to write if statement that actually uses 'test' command internal.

# Test command is mostly used to verify the expressions.

# You can use [[ ]], if you capture the output of the command inside if condition.

# ie. if [[ $(command) -eq "something" ]] and do the comparison.

# The below code is tested and does what you asked for.

if type $wordC >/dev/null 2>&1 ; then

echo "$wordC: Command exists!"

else

echo "$wordC: Command does not exist!"

fi

done

CodeScreenShot:

commandCheck.sh × commands.txt × #1 /bin/bash 3 #Reading each wo rd from file 4 for wordc in $(<commands.txt) 5 do # There is

commands.txt

clear zip
reset foo cat

Output Screenshot:

k@ashok-Aspire-E1-571G: /Desktop/Chegg ashok@ashok-Aspire-E1-571G: x ashok@ashok-Aspire-E ashok@ashok-Aspire-E1-571G:~/Deskto

Add a comment
Know the answer?
Add Answer to:
Bash Help Please. I need help with taking a txt document with commands and testing to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need this done ASAP. Please write a bash shell script that does the following: Prints...

    I need this done ASAP. Please write a bash shell script that does the following: Prints out how many users are logged on. This can be accomplished using who and wc. Prints out a list of currently logged on users. This can be accomplished using who, grep, regular expressions, and echo. Prints out how many processes you have running from past days, and a list of those jobs. Use `whoami` to get your username. Run ps -ef and see how...

  • 8. (4 pts + 2Xc) Write a bash shell script called 08-numMajors that will do the following i. Read data from a class enrollment file that will be specified on the command line ii. If the file does no...

    8. (4 pts + 2Xc) Write a bash shell script called 08-numMajors that will do the following i. Read data from a class enrollment file that will be specified on the command line ii. If the file does not exist, is a directory, or there are more or less than one parameters provided, display an appropriate error/usage message and exit gracefully Display the number of a specified major who are taking a given class iii. The following is a sample...

  • Unix questions, i need help on Hint: Commands to study to answer this question: predefined shell...

    Unix questions, i need help on Hint: Commands to study to answer this question: predefined shell variables, and .profile script file, echo SHELL, HOME, PATH, MAIL and TERM are predefined shell variables. You can use the value of a shell variable in a shell command putting $ in front of it. For example, to display the value of the HOME directory of the user, specify $HOME in the echo command like echo $HOME. Do not give just the value of...

  • I need only one  C++ function . It's C++ don't write any other language. Hello I need...

    I need only one  C++ function . It's C++ don't write any other language. Hello I need unzip function here is my zip function below. So I need the opposite function unzip. Instructions: The next tools you will build come in a pair, because one (zip) is a file compression tool, and the other (unzip) is a file decompression tool. The type of compression used here is a simple form of compression called run-length encoding (RLE). RLE is quite simple: when...

  • I need help with my homework assignment Linux systems keep user account information in the passwd...

    I need help with my homework assignment Linux systems keep user account information in the passwd file and the encrypted password in the shadow file. The passwd file containing account information might look like this: smithj:x:1001:1001:John Smith:/home/smithj:/bin/bash The shadow file containing password and account expiration information for users might look like this: smithj:KJDKKkkLLjjwlnttqoiybnm.:10063:0:99999:7::: The fields in the shadow file are separated by a colon, with the first field being the username and the second being the password. Under normal circumstances,...

  • I am a college student taking an INTRODUCTORY Windows 10 operating system SCRIPTING course and need...

    I am a college student taking an INTRODUCTORY Windows 10 operating system SCRIPTING course and need some help with creating this batch script.   I need to create the below menu: Please choose a task from the list below A) Add a new user account I need to use the SET /P command to add a new user and prompt for a login name. Check if the user already exists and if so display an appropriate message and return to the...

  • I need help with this code, I'm stuck on it, please remember step 4, I'm very...

    I need help with this code, I'm stuck on it, please remember step 4, I'm very much stuck on that part. It says something about putting how many times it appears Assignment #1: Sorting with Binary Search Tree Through this programming assignment, the students will learn to do the following: Know how to process command line arguments. 1 Perform basic file I/O. 2. Use structs, pointers, and strings. Use dynamic memory. 3. 4. This assignment asks you to sort the...

  • i need help with this please Document Q- ut References Mailings ReviewView No Spacing Heading1H security...

    i need help with this please Document Q- ut References Mailings ReviewView No Spacing Heading1H security updates, foxes, and improvements, choose Check for Updates 16. A group of services, run in a web-browser, that allows the user to easily "build" a computer in the cloud, is known as (a) PaaS (c) laas 17. A conversion design that involves the deployment of a new system in only a handful of locations when replacing an old system, while the old system still...

  • Hello I need help turning my code into a GUI applicaiton please. What I have for...

    Hello I need help turning my code into a GUI applicaiton please. What I have for code and the requirements are below. Please please help, I will be so greateful. This is my last assignment of my school and I'm just so burnt out mentally that I don't know where to start and my teacher is always to busy to help. Thank you so so so much! -----Code------ private void CalculateSC(ActionEvent event) { if(amount < 10){ chargeAmount = 1; }else...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT