LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Shell script (https://www.linuxquestions.org/questions/linux-newbie-8/shell-script-4175647798/)

sampy12345 02-07-2019 03:34 AM

Shell script
 
Hi All,

I was trying to write a script and was trying to pass one of the variable as a file name in the script but unfortunately it is not working

When i am trying to run the file manually it is working (a blank file with unknown subject line is getting created)

Can anyone please help me

Below is the script

echo -e Message:$2 \ Device:$3 \ Category:$4 \ ErrorCondition:$1 \ Generatedat:$5 >> /tmp/warning.$(date +%Y%m%d-%H%M%S%s).txt

l0f4r0 02-07-2019 03:57 AM

It looks ok to me...
Can you provide us with more context i.e. the whole script and the variable values if not confidential?

NB: you shoud probably use this instead:
Code:

echo "Message:$2  Device:$3  Category:$4  ErrorCondition:$1  Generatedat:$5" >>/tmp/warning.$(date +%Y%m%d-%H%M%S%s).txt

sampy12345 02-07-2019 04:24 AM

Quote:

Originally Posted by l0f4r0 (Post 5958914)
It looks ok to me...
Can you provide us with more context i.e. the whole script and the variable values if not confidential?

NB: you shoud probably use this instead:
Code:

echo "Message:$2  Device:$3  Category:$4  ErrorCondition:$1  Generatedat:$5" >>/tmp/warning.$(date +%Y%m%d-%H%M%S%s).txt



Even after removing the -e we are unable to run the script.

My exact requirement is I have a NMS tool (Application Manager) and whenever a alert is raised i will be calling a script and in that script i will be passing arguments which are as below
$1 as message of the alert
$2 Severity of the alert
$3 OID
.
.
.
$6 eventid
.

Here my problem is when i am calling the script a file should be created with eventid

script i configured as
echo -e "Message:$2 Device:$3 Category:$4 ErrorCondition:$1 Generatedat:$5" >>/tmp/$6

output file to be created as

In tmp folder file should be created as

eventid

This is what i am trying to achieve

l0f4r0 02-07-2019 04:41 AM

^ You mean that you don't get a new /tmp/{eventid} file after your script has run?
By the way, you should use ">>/tmp/$6" (with double quotes) and you don't need -e echo switch in your instruction (as you don't use any backslash anymore)

MadeInGermany 02-07-2019 02:45 PM

No, the >> is a redirection operator and must not be in quotes!
>> "/tmp/$6" or
>> /tmp/"$6" is okay.
It's good to have every $X in the script within double-quotes.
Further, I recommend a certain file extension.
For example
>> /tmp/"$6".event
Then later you can do operations with /tmp/*.event

l0f4r0 02-08-2019 03:30 AM

^ Of course I meant >>"/tmp/$6", not ">>/tmp/$6".
Thanks for the correction ;)

grail 02-08-2019 04:28 AM

Quote:

Not working
This is a little unhelpful. Are you saying that no file is created? or that you are getting an error?

Could you please show an exact example (contrive the information if sensitive) of calling your script including a listing of the file location before and after and
any received messages

rnturn 02-11-2019 10:45 AM

Re: "grail"'s suggestion...
 
Take a look at the script(1) command before you collect your information. Saves a lot of cutting and pasting.
Code:

$ script session.log

<issue your commands to create the "eventid" file, display its contents, etc.

^D

Then just post the contents of "session.log" into a "code" block.

For example:
Code:

$ script session.log
Script started, file is session.log
$ date
Mon Feb 11 10:42:07 CST 2019
$ uptime
 10:42  up 3 days 22:15,  9 users,  load average: 0,32, 0,54, 0,73
$ exit
Script done, file is session.log
$
$
$ cat session.log
Script started on 2019-02-11T10:42:04 CST
$ date
Mon Feb 11 10:42:07 CST 2019
$ uptime
 10:42  up 3 days 22:15,  9 users,  load average: 0,32, 0,54, 0,73
$ exit

Script done on 2019-02-11T10:42:13 CST
$

Great tool for passing test results back and forth.

HTH...


All times are GMT -5. The time now is 10:07 AM.