Loop through a directory with Grep (newbie) - bash

I'm trying to do loop through the current directory that the script resides in, which has a bunch of files that end with _list.txt I would like to grep each file name and assign it to a variable and then execute some additional commands and then move on to the next file until there are no more _list.txt files to be processed.
I assume I want something like:
while file_name=`grep "*_list.txt" *`
do
Some more code
done
But this doesn't work as expected. Any suggestions of how to accomplish this newbie task?
Thanks in advance.

If I understand you problem correctly, you don't need a grep. You can just do:
for file in *_list.txt
do
# use $file, like echo $file
done

grep is one of the most useful commands of Unix. You must comprehend it well; see some useful examples here. As far as your current requirement, I think following code will be useful:
for file in *.*
do
echo "Happy Programming"
done
In place of *.* you can also use regular expressions. For more such useful examples, see First Time Linux, or read all grep options at your terminal using man grep.

Related

bash files that do like cat

I have an exercise where I need to do a file named my_cat.sh.
It has to work in the same way as the cat command.
Ex:
∼/C-DEV-110> echo Hello > test
∼/C-DEV-110> bash my_cat.sh test
Hello
I tried to search everywhere on the Internet but couldn't find any answers.
Copy your file my_cat.sh and modify it so it takes the file to show as its first parameter.
It's the sentences if it as any other way to find an answers.
(I'm new, so it may be really simple).
I tried to simply out a cat in the nano but i doesn't give back anything.
Thank you.
The cat command copies it's arguments, or the standard input if none was specified, to the standard output.
Now, if you had to implement it using cat, your script would look like this
#!/bin/sh
cat "$#"
However, that would be trivial. Now, if you had to implement without resorting to external commands, then you can use the shell's internal (built-in) read and echo commands combined with a loop to do the job. In order to read from a file instead of the standard input you'll need to use redirections.
It should look similar to this:
while read x
do
echo "$x"
done
Something like that would be the inner loop, there will be an other loop for iterating over the input files... for that you can either use "$1" and shift with a while loop, or a for loop over "$#".

copy paste code works but not as a script

I wrote a script with six if statements that looks like this:
#!/usr/bin/bash
if [ -n $var1 ]
then
for f in /path/*.fastq.gz
do
x=${f/%.fastq.gz/_sample1-forward.fastq.gz}
y=${f/%.fastq.gz/_sample1-forward.out}
q=${f/%.fastq.gz/_temp.fastq.gz}
command [options] -i $f -o $temp${x##*/}
cp $temp${x##*/} $temp${q##*/}
done
else
echo "no $var1"
for f in /path/*.fastq.gz
do
q=${f/%.fastq.gz/_temp.fastq.gz}
cp $f $temp${q##*/}
done
fi
The other five statements do a similar task for var2 to var6. When I run the script I get unexpected output (no errors no warnings), but when I copy paste each of the if statements to terminal I end up with the exact result I would expect. I've looked for hidden characters or syntax issues for hours now. Could this be a shell issue? Script written on OSX (default zsh) and execution on server (default bash). I have a feeling this issue is similar but I couldn't find an answer to my issue in the replies.
Any and all ideas are most welcome!
Niwatori
You should maybe look at the shebang. I think proper usage would be #!/usr/bin/env bash or #!/bin/bash.
thanks for the help, much appreciated. Shebang didn't seem to be the problem although thanks for pointing that out. Shellcheck.net reminded me to use
[[ ]]
for unquoted variables, but that didn't solve the problem either. Here's what went wrong and how I 'fixed' it:
for every variable the same command (tool) is used which relies on a support file (similar in format but different content). Originally, before every if statement I replaced the support file for the previous variable with the one needed for the current variable. For some reason (curious why, any thoughts are welcome) this didn't always happen correctly.
As a quick workaround I made six versions of the tool, all with a different support file and used PYTHONPATH=/path/to/version/:$PYTHONPATH before every if statement. Best practice would be to adapt the tool so it can use different support files or an option that deals with repetitive tasks but I don't have the time at the moment.
Have a nice day,
Niwatori

nemo script for torrents

Hi I am new to scripting and I do mean a complete noobie. I am working on a script to automatically make a torrent with nemo scripts.
#!/bin/bash
DIR="$NEMO_SCRIPT_SELECTED_FILE_PATHS"
BNAME=$(basename "$DIR")
TFILE="$BNAME.torrent"
TTRACKER="http://tracker.com/announce.php"
USER="USERNAME"
transmission-create -o "/home/$USER/Desktop/$TFILE" -t $TTRACKER "$DIR"
It does not work.
However if I replace
DIR="$NEMO_SCRIPT_SELECTED_FILE_PATHS"
with
DIR="absolutepath"
than it works like a charm. It creates it on the desktop with the tracker I want. I think this would come in handy for many people. I dont really know what to put. Have questions please ask. Again complete noobie.
The $NEMO_SCRIPT_SELECTED_FILE_PATHS is the same as $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS. It's populated by nemo/nautilus when you run the script and contains a newline-delimited (I think) list of the selected files/folders. Assuming you are selecting only one file or folder, I don't really see why it wouldn't work - unless the newline character is in there and causing problems. If that's the case, you may be able to strip it with sed. Not running nemo or nautilus, so I can't test it.
I finally found the solution to yours and my problem [https://askubuntu.com/questions/243105/nautilus-scripts-nautilus-script-selected-file-paths-have-problems-with-spac][1]
The variable $NEMO_SCRIPT_SELECTED_FILE_PATH/$NAUTILUS_SCRIPT_SELECTED_FILE_PATH is a list of paths/filenames seperated by a Newline. This messes up anything that assumes its just one filename, even if it is.
#!/bin/bash
echo "$NEMO_SCRIPT_SELECTED_FILE_PATHS" | while read DIR; do
BNAME=$(basename "$DIR")
TFILE="$BNAME.torrent"
TTRACKER="http://tracker.com/announce.php"
USER="USERNAME"
transmission-create -o "/home/$USER/Desktop/$TFILE" -t $TTRACKER "$DIR"
done
Notice it seems to do an extra pass for the newline. You either need to filter that out or put an if the file/folder exists

searching in the source code

Often I do different projects and sometimes there is a lack of documentation.
So I decided to use open-source code for looking how people solved different problems.
The idea is if I run into function what I don't how to use I look for different developers used that function before.
Approach:
I downloaded a few pretty decent projects done by other people and put them into one folder.
Now, if I don't know how a function is used (e.g. main() ), I do :
find . -name \*.py | xargs cat | grep -n "main()"
Consequently I get examples of its use:
But there is a problem. I don't know from which file examples are. It'd be perfectly if it was possible to get name of the file as well as number of line.
It seems to be limitation of use "cat" command because it mixes all files together and as result I get information about number not in the file but rather in cat output. So I feel this approach is bad in the root.
i.e.
I want to be able to look for functions/symbols in plethora of source code
and get information about the line and file where a certain combination was met.
I prefer console-way.
Any advice?
Try this:
find . -name \*.py -exec grep -nH "main()" {} \;
Explanation:
The "-exec" option says to execute the following command, up until \; for each file it finds.
The "-H" option to grep causes it to print the name of the file in which the string was found.
The "-n" option causes grep to print the line numbers.
The {} is a placeholder that expands to the name of the file that "find" just found.
You need only grep command:
$ grep -nr 'main()' /path/to/projects/folder/* | grep '.py:'
Want to search source files ? Why not http://beyondgrep.com/ ?
I wont answer you from the point of the bash.
I dont know which editor/IDE are you using, but for code dissecting there is no better tool for me then:
Vim with Ctags combination
Ctrl-p,Ctrl-p funky and MRU plugin +
proper search and regex usage.
good vim debugger
There is no part of code that cant be examined. Please sorry if are using some other tools, I am just suggesting you what do I find is the best for code analysis for me.

Scanning text file line by line and reading line with unix shell script

I know there are a lot of different threads already like this, but nothing I can find seems to explain well enough exactly what I'm trying to do.
Basically I want to have a shell script that just goes through a text file, line by line, and searches for the words "Error" or "Exception". Whenever it comes across those words it would record the line number so I can later shoot the text file off in an email with the problem lines.
I've seen a lot of stuff that explains how to loop through a text file line by line, but I don't understand how I can run a regular expression on that line, because I'm not sure exactly how to use regular expressions with a shell script and also what variable each line is being stored in...
If anybody can clarify these things for me I would really appreciate it.
There are numerous tools that automatically loop thru files. I would suggest a simple solution like:
grep -inE 'error|exception' logfile > /tmp/logSearch.$$
if [[ -s /tmp/logSearch.$$ ]] ;then
mailx -s "errors in Log" < /tmp/logSearch.$$
fi
/bin/rm /tmp/logSearch.$$
use man grep to understand the options I'm supplying.
From `man bash
-s file
True if file exists and has a size greater than zero.
I hope this helps.
You need to look into using the grep command. With it you can search for a specific string, output the line number and the line itself, and do much much more.
Here is a link to a site with practical examples of using the command: http://www.thegeekstuff.com/2009/03/15-practical-unix-grep-command-examples/
Point #15 in the article may be of special interest to you.

Resources