Sponsored Content
Top Forums Shell Programming and Scripting Parsing special characters from variable commands Post 302884359 by joeniks on Monday 20th of January 2014 06:07:01 AM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
service(8)						      System Manager's Manual							service(8)

NAME
service - run a System V init script SYNOPSIS
service SCRIPT COMMAND [OPTIONS] service --status-all service --help | -h | --version DESCRIPTION
service runs a System V init script in as predictable environment as possible, removing most environment variables and with current working directory set to /. The SCRIPT parameter specifies a System V init script, located in /etc/init.d/SCRIPT. The supported values of COMMAND depend on the invoked script, service passes COMMAND and OPTIONS it to the init script unmodified. All scripts should support at least the start and stop commands. As a special case, if COMMAND is --full-restart, the script is run twice, first with the stop command, then with the start command. service --status-all runs all init scripts, in alphabetical order, with the status command. EXIT CODES
service calls the init script and returns the status returned by it. FILES
/etc/init.d The directory containing System V init scripts. ENVIRONMENT
LANG, TERM The only environment variables passed to the init scripts. SEE ALSO
/etc/init.d/skeleton, update-rc.d(8), init(8), invoke-rc.d(8). Jan 2006 service(8)
All times are GMT -4. The time now is 01:23 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy