Parsing special characters from variable commands


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parsing special characters from variable commands
# 1  
Old 01-20-2014
Parsing special characters from variable commands

Hi,
I am fairly new to unix scripting and recently tasked with some reporting scripts.

The reporting checks several batch jobs and this is quite iterative.
Now I am trying to minimize script effort and maximize reusability as there are only slight nuances in the repetitive tasks.

For example I am herebelow trying to find the latest logfile, but for example the name is not always the same.
and other logfiles may exist in the same folder which are not interesting.
So I came up with parsing from a command (which also may differ on what you are trying to do.
now I am having some difficulty understanding why some of the parsing fails.

So I was hoping for some expert explanation, just for my understanding.

Any help would be appreciated
fyi, working in bash 3 on Solaris 11.
This fails (and I dont understand why)
Code:
#!/bin/bash
SHARED=/somesharedfiledir
m_jobstart=$SHARED/dummyfile_i_timestamped
apply_cmd_ls='ls -rat '$SHARED'/batches/var/log/ | tail -1'
apply_cmd_find='find '$SHARED'/batches/var/log/ -type f \( -name "*mainlog.log" \)-newer '$m_jobstart
function findlog {
 unset reply
 unset result
 unset apply
 reply=apply_cmd_$1
 result=`${!reply}`
 echo "result: "$result
}
findlog 'find'

obviously what I am trying to do is more complex, but I am trying to understand why it fails on the \(
This works
Code:
#!/bin/bash
SHARED=/somesharedfiledir
m_jobstart=$SHARED/dummyfile_i_timestamped
apply_cmd_ls='ls -rat '$SHARED'/batches/var/log/ | tail -1'
apply_cmd_find='find '$SHARED'/%B/var/log/ -type f %f -newer '$m_jobstart
function findlog {
 unset reply
 unset result
 unset apply
 # using $3 as a filter here
 reply=apply_$1
 apply=`echo ${!reply} | sed "s#%B#${2}#g;s#%f#${3}#g"`
 result=`$apply`
 echo "result: "$result
}
findlog 'find' 'batch1' '-name *mainlog.log'
findlog 'find' 'batch2' '-name *mainlog.log'
findlog 'find' 'batch3' '\( -name *mainlog.log ! -name *LB71*.log \)'
findlog 'ls' 'batch4'

general pointers on approach are welcome as well Smilie
thanks

Last edited by joeniks; 01-20-2014 at 08:06 AM.. Reason: typo in code2
# 2  
Old 01-20-2014
Just a general comment, "minimize script effort and maximize reusability" are subjective. When I write a new script my goals are [1]efficiency in processing (for example reduce the time going through a file if parsing) and [2] is this easy for someone to understand that may have to modify it. I also see you are assigning the output of ls and find to a variable. Depending on the amount of data, this could exceed the size of a variable.
# 3  
Old 01-20-2014
Don't put code in variables. That's just gross. Why not use case?

Code:
#!/bin/bash
SHARED=/somesharedfiledir
m_jobstart=$SHARED/dummyfile_i_timestamped


findlog() {
 local reply result apply

 case "$1" in
     ls) result=$(ls -rat "$SHARED/batches/var/log/" | tail -1)
         ;;
   find) result=$(find "$SHARED/$2/var/log" -type f "$3" -newer "$m_jobstart")
         ;;
 esac
}

This just takes care of using variables of code. There's still the issue of is this the best way to find the log ..
These 2 Users Gave Thanks to neutronscott For This Post:
# 4  
Old 01-20-2014
Thanks,
Gross is fun !!!

No seriosuly thank you, that is probably an equally good approach, probs was just overengineering it.
a case statement teeny bit less flexible than what I was trying to achieve, but the script will indeed be more readable anyways.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parsing a file containing special characters

I want to parse a file containing special characters, below is a sample content of file content of file : Serial_no:1$$@#first_name:Rahane$$@last_name:Ajiyenke@@#profession:cricketer!@#*&^ Serial_no:1$$@#first_name:Rahane$$@last_name:Ajiyenke@@#profession:cricketer!@#*&^... (3 Replies)
Discussion started by: rajMjar
3 Replies

2. Shell Programming and Scripting

awk match shell variable that contains special characters?

How to match a shell variable that contains parenthesis (and other special characters like "!") file.txt contains: Charles Dickens Matthew Lewis (writer) name="Matthew Lewis (writer)"; awk -v na="$name" ' $0 ~ na' file.txt Ideally this would match $name in file.txt (in this... (3 Replies)
Discussion started by: Mid Ocean
3 Replies

3. Shell Programming and Scripting

Trouble with sed and substituting a string with special characters in variable

Hey guys, I know that title is a mouthful - I'll try to better explain my struggles a little better... What I'm trying to do is: 1. Query a db and output to a file, a list of column data. 2. Then, for each line in this file, repeat these values but wrap them with: ITEM{ ... (3 Replies)
Discussion started by: ampsys
3 Replies

4. UNIX for Dummies Questions & Answers

Environment Variable with Special characters

Hello all, I am facing with a problem of invoking an environment variable. If I use this command : /bin/ls -lt FILE_NM_?(20120515|20120516)??????_sas.sig | head -n1 | awk '{print $9}' It perfectly outputs the desired result. FILE_NM_20120516000000_sas.sig But if I do this:... (8 Replies)
Discussion started by: sethmj
8 Replies

5. Shell Programming and Scripting

Replace special characters with Escape characters?

i need to replace the any special characters with escape characters like below. test!=123-> test\!\=123 !@#$%^&*()-= to be replaced by \!\@\#\$\%\^\&\*\(\)\-\= (8 Replies)
Discussion started by: laknar
8 Replies

6. Shell Programming and Scripting

Sed failing for variable with special characters

This has been covered many times earlier but couldnt figure the issue myself. Can you please advise whats wrong on the below code I have a variable with special character ($) and am using that variable to replace another variable in file but however sed is failing to parse it correctly ... (7 Replies)
Discussion started by: sasiharitha
7 Replies

7. Shell Programming and Scripting

Sed special parsing

What is the shortest & right way to remove the string "" with a sed statement ? echo 'whateverwhatever' | sed ........ ? :) (2 Replies)
Discussion started by: ctsgnb
2 Replies

8. Shell Programming and Scripting

Special characters in a bash variable in sed

Hello, I am trying the following: echo __CHANGEME__ >> testfile VAR1="&&&" sed -i "s|__CHANGEME__|${VAR1}|" testfile cat testfile This results in testfile containing __CHANGEME____CHANGEME____CHANGEME__ Whereas I want it to result in &&& I understand that if VAR1="\&\&\&" then... (3 Replies)
Discussion started by: linuxnewbeee
3 Replies

9. UNIX for Dummies Questions & Answers

Parsing special characters between C and XML..

Hi, I am getting problem in parsing special characters(Like &, > or <) in XML. I need to encode my C program and send in report format to another interface which is in XML format. I do not know how to encode these special characters in C program before sending to XML format. Please help !! (1 Reply)
Discussion started by: ronix007
1 Replies

10. Shell Programming and Scripting

Variable Manimpulation - special characters

Hello- I have a variables that contains a string like this usr/pass@SCHEMA I want to extract the usr/pass part and ignore the SCHEMA part, I tried to use this ${dbconn%%@} and apparently it will not work because @ is a special character. I tried \@ and still no go. Any idea how to solve... (1 Reply)
Discussion started by: Nomaad
1 Replies
Login or Register to Ask a Question