Scripting & Pipe Help
Hello long time lurker and new to Linux.
I am using CentOs 6.4 and I need some help setting up a script. Here is what I am looking to automate and below is a sample of the script I setup. 1) looking to pipe the output of the data to a variable file each time the script is run. For example currently I have to run ./erase.sh >> SNxxxxxxx.txt. However optimally I would like to run just ./erase.sh and have the script file look at the output from the script and grab the SNxxxxxxx and time stamp the output file automatically. 2)Can a run a script inside a script? I think I can but just making sure. 3)Is there a way to output to screen and pipe the information at the same not. Not a big deal just wondering. Here is the sample script<erase.sh> ddcli is a command for the card. #!/bin/bash # rsync using variables SOURCEDIR=/home/user/Desktop/ ./ddcli -c 4 -list echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=Initial LIST complete=-=-=-=-=-=-=-=-=-=-=-=-=-=" ./ddcli -c 4 -health echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=Initial HEALTH complete=-=-=-=-=-=-=-=-=-=-=-=-=-=" ./ddcli -c 4 -format -eraseflash -s echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=FORMAT SECUREERASE complete=-=-=-=-=-=-=-=-=-=-=-=-=-=" ./ddcli -c 4 -list echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=Final LIST complete=-=-=-=-=-=-=-=-=-=-=-=-=-=" ./ddcli -c 4 -health echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=Final HEALTH complete=-=-=-=-=-=-=-=-=-=-=-=-=-=" essentially this is a command to pull data from storage card I have. Thank you in advance for the help. |
Quote:
Quote:
Quote:
Code:
echo "send to tee" | tee Log.log not much to read, but read anyway Code:
man tee Quote:
[code] Your code here [/code] not much for me to say about that script only that you might want to make it 'position independent' that is, hardcode it to the location of ddcli one way is to cd into the dir that contains ddcli another is to use the full path, that assumes the ddcli script/program is hardcoded with relevant paths also it will 'carry on regardless' of any failure, since I can't see ddcli, I don't know if you can make it 'smarter' assuming ddcli uses exit codes ( with 0 meaning success and none-zero being failure ) Code:
em="=-=-=-=-=-=-=-=-=-=-=-=-=-=" basically it is this Code:
em="=-=-=-=-=-=-=-=-=-=-=-=-=-=" # makes life easier later http://www.tldp.org/LDP/Bash-Beginners-Guide/html/ http://www.tldp.org/LDP/abs/html/ http://mywiki.wooledge.org/BashGuide http://www.gnu.org/software/bash/manual/bashref.html The tldp stuff is great, however there are some nasty bad habits in it, The mywiki.wooledge does a very good job of 'fixing' those habits |
Hi Firerat thank you for the quick reply. Sorry about not putting the code in a box...
I am still unclear about item 1. I think I get the jist of item 3. Again, sorry some of what you said is still above my head. Code:
#!/bin/bash -- run all 5 commands (via the script) automatically output to a file called temp_output.txt -- read the output and automatically get the serial number from the temp_output.txt -- automatically rename the file temp_output to SN_SP24447758_<time stamp>.txt I think my biggest question is how to automatically rename temp_output.txt to SN_SPxxxxxxxx_<timestamp>.txt. The problem is I do not know what to put in a script to read the file, get the board tracer number, rename that file with board tracer number and time stamp to a text file. Here is a sample of the output from ddcli Code:
WarpDrive Selected is NWD-RLP4-1860 Thanks again for your help |
not clear when you get that output
Code:
#!/bin/bash Warning: I have made no effort to use correct paths I don't know if "Board Tracer Number" will appear more than once ( actually, just added the ;exit to the awk, so after first result it exits ) |
1 Attachment(s)
you are very gracious for doing the script.
I get a SN_2013-10-260193116.txt (great) file but it is blank, also the filename did not pick up the board tracer number. Could it be because we are not piping the output to any file. I see the output on the screen. I grabbed all the output and attached the text file. I made a minor modification to the script to comment out the eraseflash option. Just to test it out... The eraseflash option takes too long. I think we are almost there... |
well, if those ./ddcli commands do not print to stdout, then tee will not be able to append them
I'm blind as I can't see what ddcli does if you see the relevant text 'fly by' when running the script it could that it is sending it to stderr change LogIt="|tee -a $MyTemp" to Code:
LogIt=" 2>&1 | tee -a $MyTemp" tee should then be appending it to the tmpfile, |
Quote:
If I were to run ddcli -c 1 -list in terminal --> the utility selects the card 1 and displays the list (informational) output of the card (ie FW,and other device information)to the terminal window. If I want to save it to a file I would do a ddcli -c 1 -list >> list_output.txt, as an example. Like doing ls command it would output to termianl windows and if I did a ls >> dir.txt it would output the directory content to a file called dir.txt. BTW I under stand that doing a ">>" will continue to append to that one file. Quote:
Quote:
Thank you |
ok, well I don't understand why the file was blank
lets keep it simple and eliminate tee ( it might not be installed? ) LogIt=">> $MyTemp" |
I think we are almost there.
I modified the code slightly, I pointed to the $MyTemp. The output file is correct. The only problem is the output does not "fly by" on the screen. I can only see my echo commands. Here is the modified code: Code:
#!/bin/bash Code:
./1C_Securerase.sh SN_SP32611130_2013-10-26-203612.txt |
yes, the reason you do not see the ddcli script output is because it is being sent to the temp file
at the moment I assume tee is not installed, another way of doing what tee does is to start tail in the background Code:
MyTemp=$(mktemp) the $! is the PID of the last background process we get that so we can 'cleanup' before the script exits |
Quote:
PERFECT!!! Thank you very much for you help, I tied to rep you but it looks like you have helped many. Many thanks!!! We can mark this as [SOLVED]. |
All times are GMT -5. The time now is 12:11 AM. |