Automate multiple commands


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Automate multiple commands
# 1  
Old 06-02-2013
Automate multiple commands

Hi,
I am trying to count the number of times a string of letters occurs in a file for multiple unique strings of letters. Right now I can do this one at a time using the following code (in this example I am searching for the string "AAA"):
Code:
echo AAA >> outfile.txt
grep -c "AAA" -r file_to_search.txt >> outfile.txt

The issue is that I have several thousand of these unique strings of letters to search for and would like to automate things a bit. I have tried adding a #!/bin/bash to the beginning of a file containing multiple search terms but nothing happens when I execute the file. Here is an example:
Code:
#!/bin/bash
echo AAA >> outfile.txt
grep -c "AAA" -r file_to_search.txt >> outfile.txt
echo AAB >> outfile.txt
grep -c "AAB" -r file_to_search.txt >> outfile.txt
echo AAC >> outfile.txt
grep -c "AAC" -r file_to_search.txt >> outfile.txt

I am very much a novice and would appreciate any ideas or hints.
Thanks!
# 2  
Old 06-02-2013
Quote:
Originally Posted by gecko1
... but nothing happens when I execute the file. ...
Code:
#!/bin/bash
echo AAA >> outfile.txt
grep -c "AAA" -r file_to_search.txt >> outfile.txt
echo AAB >> outfile.txt
grep -c "AAB" -r file_to_search.txt >> outfile.txt
echo AAC >> outfile.txt
grep -c "AAC" -r file_to_search.txt >> outfile.txt

...
Most likely, something does happen when you execute your script. But since nothing is printed on your terminal, you feel as if nothing happens.
Every command in your script is followed by the redirection operator (the ">>") and hence the output of every command is redirected to the file "outfile.txt". So, by the time the script finishes its execution, the "outfile.txt" gets filled up.
You can check that by printing the content of "outfile.txt" after executing your script.
Code:
cat outfile.txt

# 3  
Old 06-02-2013
durden_tyler,
Thank you for your response. I have checked and the outfile.txt is not created suggesting nothing is run. I've gone ahead and just cut and pasted my commands into the terminal and got things finished but moving forward I'd like to get this working.
Thanks!
# 4  
Old 06-03-2013
Post some of the file_to_search.txt
# 5  
Old 06-03-2013
The outfile.txt must atleast contain the below three lines since you are appending these strings.
Quote:
AAA
AAB
AAC
Try debugging the script by adding 'set -x', just after the first line and run it.

Paste here the terminal o/p.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Open Source

Help with writing Shell Script to automate process using multiple commands

Hello! Need help to write a Linux script that can be run from windows using command/Cygwin/any other way. I am new to scripting, actually i am trying to automate server health check like free disk space, memory along with few services status, if any services is not running then start services ,... (7 Replies)
Discussion started by: Sayed Ibrahim
7 Replies

2. UNIX for Advanced & Expert Users

Pass Multiple Commands and Open Multiple Xterms via PSS

Hello, I'm attempting to open multiple xterms and run a command as an SAP user via sudo using PSSH. So far, I'm able to run PSSH to a file of servers with no check for keys, open every xterm in to the servers in the file list, and SUDO to the SAP(ADM) user, but it won't do anything else... (11 Replies)
Discussion started by: icemanj
11 Replies

3. Shell Programming and Scripting

Automate an application using scripting, managing multiple terminals

Hi I am new to Ubuntu and Bash scripting. I am working on a project to give a demo on an SDN application to my class. I need some help in scripting to create the demo. Please help in case if you have any idea on what am asking. The demo uses a tool called mininet. I need just one script so... (2 Replies)
Discussion started by: anzal
2 Replies

4. Shell Programming and Scripting

Need Help with commands to automate.

HI, In some test cases, I used tools like "dd" and "shed" to manually read a block from the disk, modify it using a hex editor and write it back using "dd". I need help with some linux commands I can use to read the block and change the data to induce the corruption....as I want to automate my... (3 Replies)
Discussion started by: prinsh
3 Replies

5. Shell Programming and Scripting

Bash Scripting Help to automate replacing multiple lines

Background: I am writing a script to help me automate tweaks and things I apply to a custom Android rom I developed. I am on the very last part of my script, and I am stuck trying to find the right command to do what I seek. When I build roms from source, a file called updater-script is... (8 Replies)
Discussion started by: Silverlink34
8 Replies

6. Shell Programming and Scripting

perform 3 awk commands to multiple files in multiple directories

Hi, I have a directory /home/datasets/ which contains a bunch (720) of subdirectories called hour_1/ hour_2/ etc..etc.. in each of these there is a single text file called (hour_1.txt in hour_1/ , hour_2.txt for hour_2/ etc..etc..) and i would like to do some text processing in them. Each of... (20 Replies)
Discussion started by: amarn
20 Replies

7. Solaris

Help with executing multiple remote commands after multiple hops

Hi SSHers, I have embedded this below code in my shell script.. /usr/bin/ssh -t $USER@$SERVER1 /usr/bin/ssh $USER2@S$SERVER2 echo uptime:`/opt/OV/bin/snmpget -r 0 -t 60 $nodeName system.3.0 | cut -d: -f3-5` SSH to both these servers are public-key authenticated, so things run... (13 Replies)
Discussion started by: LinuxUser2008
13 Replies

8. Shell Programming and Scripting

multiple commands

Hi, I have multiple commands that i need to run daily on my Solaris servers and watch the output, and actually i do it as multi-tasking. I do not want to put all of them in the file, and run it, and get the entire output at one time. What i want is that it should ask for before it runs next... (5 Replies)
Discussion started by: john_prince
5 Replies

9. Shell Programming and Scripting

automate ftpget to multiple hosts

Hi Folks, I have scanned the threads all day and have not found anything close enugh to what I need. I'm probably more confused now than before. Here's what I'm trying to do: 1. automate for running in the early am. (I think I can handle the cron part) 2. get the newest file from a... (5 Replies)
Discussion started by: huntfishtrap
5 Replies

10. UNIX for Advanced & Expert Users

Automate Script using pkgrm/pkgadd commands

This is my first post so hello to all of you! I have a script in bash-3.00 that needs to execute the following: pkgrm (to remove an existing package) pkgadd -d ( to install a new package) The problem is that both commands prompt me to answer either y,n or q in order to proceed with... (13 Replies)
Discussion started by: Insight
13 Replies
Login or Register to Ask a Question