Shell Script function to use script name for log file output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Script function to use script name for log file output
# 8  
Old 04-03-2016
As there are no periods in your script name apart from the suffix, yes this will work. Obviously as you have seen though, there is no need to hard code your script name within your script itself. I would definitely recommend enclosing your variables within curly braces as I mentioned in the previous post. For example: ${_INTRA_PATH}
# 9  
Old 04-03-2016
Pilent -

Okay thank you. Sorry, i'm very new to shell scripting. What is the advantage of the curly braces? And Should each variable receive them?

For instance:

Code:
_LOGFILE=${_INTRA_PATH}/${_DATETIMESTAMP}_${_SN%%.sh*}.log


Last edited by RudiC; 04-04-2016 at 03:47 AM.. Reason: Code tags...
# 10  
Old 04-03-2016
Apart from looking a lot cleaner and making your code much more readable, enclosing your variable names with curly braces is good practice as you are likely to encounter situations where curly braces are required, for example:
  • Referencing positional parameters beyond 9 (e.g. ${10} not $10)
  • Utilising parameter expansion
  • Expanding arrays (e.g. ${ARRAY[5]})
Most importantly, in your code your "$_LOGFILE" variable will actually break unless you enclose your "$_DATETIMESTAMP" variable with curly braces (as you have done in your recent post). Otherwise it will try to find a variable called "$_DATETIMESTAMP_" which does not exist.
# 11  
Old 04-03-2016
Pilent -

So best practice is to enclose every variable like that? Awesome! Thank you very much, I'll adjust all my scripts now.

Thank you again for all your help!
# 12  
Old 04-03-2016
Yes I would say so and no problem.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to pass the config file lines as variable on the respective called function on a script

I want to make a config file which contain all the paths. i want to read the config file line by line and pass as an argument on my below function. Replace all the path with reading config path line by line and pass in respective functions. how can i achieve that? Kindly guide. ... (6 Replies)
Discussion started by: sadique.manzar
6 Replies

2. Programming

Writing a UNIX shell script to call a C function and redirecting data to a .txt file

Hi, I am complete new to C programming and shell scripting. I just wrote a simple C code to calculate integral using trapezoid rule. I am prompting user to pass me No. of equally spaced points , N , upper and lower limit. My code looks as follows so far: #include<stdio.h> #include<string.h>... (2 Replies)
Discussion started by: bjhjh
2 Replies

3. Shell Programming and Scripting

What is the function of the following lines at the top of a shell script file: Directory and Script?

The file starts like this: Directory: <path to the script> Script: <script fife name> #!bin/ksh ##Comments <actual script> What is the use of the first two lines in the script? What if I save the file without them? What will be the effect? They are not comments. Im very new to this,... (4 Replies)
Discussion started by: remytom
4 Replies

4. Programming

How to redirect the output of a shell script to a file?

hi, i have a html form which call a perl program, this perl program calls a shell script. <html> <head> <title>demo</title> </head> <body> <form name="frm1" action="/cgi-bin/perl_script.pl" method="post"> <input type="text" name="fname"> ... (1 Reply)
Discussion started by: Little
1 Replies

5. Shell Programming and Scripting

script to mail monitoring output if required or redirect output to log file

Below script perfectly works, giving below mail output. BUT, I want to make the script mail only if there are any D-Defined/T-Transition/B-Broken State WPARs and also to copy the output generated during monitoring to a temporary log file, which gets cleaned up every week. Need suggestions. ... (4 Replies)
Discussion started by: aix_admin_007
4 Replies

6. Shell Programming and Scripting

Calling a function in cpp file inside shell script

Hi I need to call a function written in a cpp file with arguments inside the shell script..Can anyone help me how to do this:( (1 Reply)
Discussion started by: rkrish
1 Replies

7. Shell Programming and Scripting

Call shell script function from awk script

hi everyone i am trying to do this bash> cat abc.sh deepak() { echo Deepak } deepak bash>./abc.sh Deepak so it is giving me write simply i created a func and it worked now i modified it like this way bash> cat abc.sh (2 Replies)
Discussion started by: aishsimplesweet
2 Replies

8. Shell Programming and Scripting

getting garbage in the output file of shell script

Hi Everyone, The problem is that I am getting messages other than the script in the current log file. Ideally the script should contain only the messages that are redirected to the log file. How to remove these unwanted data from the log file. Please help if you have any idea how to remove the... (0 Replies)
Discussion started by: vsachan
0 Replies

9. Shell Programming and Scripting

Execution Output of a shell script into a file.

Hi Experts, I have a script called test.sh. I am trying to execute it with sh -x test.sh. Where i can find sequence of steps executed one by one. Now i want to these executions to be captured in a file. i.e sh -x test.sh > output.txt the above one is notworking. can anyone help me... (6 Replies)
Discussion started by: naree
6 Replies

10. Shell Programming and Scripting

extracting function headers in a c/c++ file using shell script

Hi, Is there any way to extract function headers from c and c++ files using a shell script? I tried to do it by reading the C/C++ file line by line and if a line matches a particular pattern (pattern of function header) i extracted it otherwise moved to next line. The problem here is, some... (3 Replies)
Discussion started by: priyadarshini
3 Replies
Login or Register to Ask a Question