I'm writing script to find out last files and its modified date - unfortunately am having problem with the below script.
Error message:
It might help to properly indent your code. For the machine it doesn't matter but it makes for easier reading.
OK, here are a few pointers to make your code better:
1) get rid of the backticks! Their use is strongly discouraged and they only exist for backwards compatibility. Just because the shell is lenient enough to understand them is no reason for you to use them. Use process substitution ("$( ... )") instead.
2) The shell can do integer arithmetics itself! There is no need to call upon external programs to do what the shell can do. This only slows down your script because calls to external programs require the fork()-system call which is expensive. Instead of "let", "expr" just use "(( ... ))".
3) Declare what you use! Yes, it is possible to use variables just like that. No, this still is not good practice. By first declaring your variables you can give them default values, make sure they are of the right type AND you have the chance of annotating your code. This way you get documentation automatically.
4) Quote your variables! To protect your code of shell interpretation if your variables contain white space you should routinely quote them. Instead of, for instance, grep ^$prod use grep "^${prod}"
5) While this all was not making your code fail the following probably was. I marked bold the problem:
grep is called like this: "grep regexp /path/to/file" and the comma is probably not part of the regexp you look for.
Further, you could do yourself a favor and grep for the right line first, then store it to a variable instead of retrieving it over and over again:
Further suggestions: get some system into your variable naming. It is easy to lose track between upper case lower case, mixed case, etc. identifiers. Personally, i use a (stripped down) sort-of Hungarian Style Notation, but that is more a matter of taste. If i read in a record (like you do with what i called "workline") I would have the resulting variables named to reflect where they come from:
("ch" ("char") is prefix for strings, the last one uses "f" as a prefix because "f" ("file") means a path name for me, so i immediately recognize the variables content just by reading its name). As they all start with "Line" i know where they come from. The form a sort-of "structure" (to [mis-]use that term from the C language).
Dear all,
Please help with the following.
I have a file, let's call it data.txt, that has 3 columns and approx 700,000 lines, and looks like this:
rs1234 A C
rs1236 T G
rs2345 G T
Please use code tags as required by forum rules!
I have a second file, called reference.txt,... (1 Reply)
Hi,
I am trying to run a python script embedded in bash script. But is throwing me an error. Please help.
Script:
#!/bin/bash
nohup /usr/bin/python /opt/web/http.py &
Error:
/usr/bin/python: can't open file '/opt/web/http.py': No such file or directory
Please help me on this. (6 Replies)
Hi,
I'm having trouble with a script to copy one line out of multiple files in a directory and copy to a file called test. I've tried the code below but it copies one line out of the first file multiple times not one line out of all the files. Would someone help? I'm very new to all this.
... (8 Replies)
Hello,
I am writing a script that has to log in to X number of servers over a full C class range.
I am only keyed on a certain number of these servers and this can change from time to time.
The part of my script that deals with this is
for i in $(cat $server_list); do
ssh ... (0 Replies)
When it comes to programing and UNIX, I know just enough to be really really dangerous.
I have written a python script to parse through a file that contains ~1 million lines. Depending on whether a certain string is matched, the line is copied into a particular file. For the sake of brevity,... (4 Replies)
hi guys
i have this very messy script, that looks in /var/log/messages.all for an error and reports if it finds the key works
how can i get it to look at more then one file, i.e /var/log/message.all *
so it looks in old logs as well
thanks
exit 0 if (isRenderNode(hostname));
my... (4 Replies)
Hi,
I am very new to bash scripting and I need to write a bash script that takes two arguments, a string and a file. The output should be each line which matches the string *from the beginning of the line*. For example, given a string "ANA" the line starting with "ANABEL" will be printed, but... (9 Replies)
I'm tring to write down a simple script that would execute a command and wait until it returns a specific result.
This is what i did:
bjobs_out=`bjobs`
while ]; do
bjobs_out=`bjobs`
sleep 6
done
It seems to work until the command 'jobs' return the list of jobs in execution, but... (4 Replies)
Hi,
I confess to be stuck ...
as explained here
Aliases - Bash Reference Manual
I added
shopt -s expand_aliasesto my script ... but it didn't work.
Later I've read I had to add this at the begning of my my script :
. ~/.bash_aliases(of course my aliases are in this file)
but again, the... (0 Replies)
I'm trying to make a simple search script but cannot get it right. The script should search for keywords inside files. Then return the file paths in a variable. (Each file path separated with \n).
#!/bin/bash
SEARCHQUERY="searchword1 searchword2 searchword3";
for WORD in $SEARCHQUERY
do
... (6 Replies)