LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-31-2018, 10:55 AM   #1
storms
LQ Newbie
 
Registered: Feb 2017
Posts: 11

Rep: Reputation: Disabled
how to make every script action into a log file.


Hello!
so i'm doing a script for college, and i'm almost over, but i need to add a system to write every script action into a log file.
I already looked for solutions and one solution that i found and is NOT what i'm looking for is calling the script with exec > /tmp/myLog.log 2>&1.
what i want is an way to incorporate the write to log into the script!
so enought talking heres what i got so far:

Code:
 #!/bin/bash
 2 #Purpose = Safe delete
 3 #Created on 20-03-2018
 4 #Version 0.120
 5 #jesus,i'm dumb
 6 #START
 7
 8 ##Constants
 9 dir="/home/cunha/LIXO"
10 sdel="/home/cunha/sistemas_operativos/sdel.sh"
11
12
13 ##MAIN
14 while [[ ! -z "$1" ]]; do
15         case "$1" in
16                 -h)     echo "-------------------------------------------------------------------------------------------------------"
17                                                 echo "Option            Meaning"
18                                                 echo "-h                HELP MENU"
19                         echo "-r <dir>          Sdel is applied recursivly in the input directory."
20                         echo "-s <value>        Files with the value(KB) in /home/cunha/LIXO  above the input number are deleted."
21                         echo "-t <value>        File with the value(time/hours) in /home/cunha/LIXO above the input number are deleted."
22                         echo "-u                Displays the largest file in /home/cunha/LIXO."
23                                 echo "-------------------------------------------------------------------------------------------------------"
24                                 shift
25                 ;;
26                 -r) cd "$2"
27                 ls >/dev/null
28                 for i in *; do
29                 fwe="${i%.*}" #file_without_extension
30                 if [[ -f "$dir/$fwe.tar.bz2" ]]; then
31                         if [[ "$i" -nt "$3" ]]; then
32                                 echo "File already exists, removing older file." && rm "$dir"/"$fwe.tar.bz2" && tar -czPf "$fwe.tar.bz2" "$(pwd)" && mv "$fwe.tar.bz2" "$dir"
33                         fi
34                 else
35                 if [[ -d "$i" ]]; then
36                         bash "$sdel" -r "$i"
37                 else
38                 echo "Ziping $i and moving." &&  tar -czPf "$fwe.tar.bz2" "$(pwd)" && mv "$fwe.tar.bz2" "$dir"
39                 fi
40                 fi
41                 done
42                 shift
43                 ;;
44                 -s) find "$dir" -size +"$2"k -exec rm {} \;
45                 shift
46                 ;;
47                 -t)     tempo="$(($2/24))"
48                         find "$dir" -mtime +"$tempo" -type f -exec rm {} \;
49                 shift
50                 ;;
51                 -u) find "$dir" -type f  -printf "%s %p\n" |sort -nr |head -n 1 #find no diretorio maior ficheiro, mostra em bites.
52                 shift
53                 ;;
54                 *) if ! [[ -f "$1" ]]; then
55                 echo "$1 is NOT a file."
56                 shift
57                 else
58                 for file in "$@"; do
59                 fwe="${file%.*}" #file_without_extension
60                         if [[ -f "$dir/$fwe.tar.bz2" ]]; then
61                                 if [[ "$file" -nt "$2" ]]; then
62                                 echo "File already exists, removing older file." && rm "$dir"/"$fwe.tar.bz2" && tar -czPf "$fwe.tar.bz2" "$(pwd)" && mv "$fwe.tar.bz2" "$dir"
63                                 fi
64                         else
65                         echo "Ziping it and moving." &&  tar -czPf "$fwe.tar.bz2" "$(pwd)" && mv "$fwe.tar.bz2" "$dir"
66                         fi
67                 done
68                 fi
69                 ;;
70         esac
71     shift
72 done
 
Old 03-31-2018, 11:19 AM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro
Posts: 6,397

Rep: Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238
Quote:
Originally Posted by storms
so i'm doing a script for college, and i'm almost over,...
almost over with what the collage course or this script?

Quote:
Originally Posted by storms
t i want is an way to incorporate the write to log into the script!
as in put the output into /var/log/script_output? add root privs to it, then direct it to the dir with a log file name.

Last edited by BW-userx; 03-31-2018 at 12:10 PM.
 
Old 03-31-2018, 11:31 AM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 11,000

Rep: Reputation: 3270Reputation: 3270Reputation: 3270Reputation: 3270Reputation: 3270Reputation: 3270Reputation: 3270Reputation: 3270Reputation: 3270Reputation: 3270Reputation: 3270
Quote:
Originally Posted by storms View Post
what i want is an way to incorporate the write to log into the script!
Yes, what do you mean by that?

Quote:
Originally Posted by storms View Post
exec > /tmp/myLog.log 2>&1.
So what is the problem with this line? Why don't you put it into that script?
 
Old 03-31-2018, 11:41 AM   #4
storms
LQ Newbie
 
Registered: Feb 2017
Posts: 11

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
almost over with what the collage course or this script?
the script! missing 2 years to finish college, studying Systems and Networking Management (dont rly know how to translate it xD)

Quote:
Originally Posted by BW-userx View Post
as it put the output into /var/log/script_output? add root privs to it, then direct it to the dir with a log file name.
you talking about
Code:
#!/bin/bash
{ 
script
} >/sdel.log
right? my problem with that is that i have several echos to give information about what the script is doing and when i do that all the echos are redirected.

Quote:
So what is the problem with this line? Why don't you put it into that script?
because the way the script is done and the way is suppose to be used,
ex: i'm calling the script ./sdel.sh -r /home/cunha/LIXO , my teacher doesnt want us to call the log file when executing the script, the script is suppose to manage automatically a log file when used ./sdel.sh
 
Old 03-31-2018, 12:21 PM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro
Posts: 6,397

Rep: Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238
you're going to have to play around with it to figure it out, their also maybe a different way about this as well that is better using tee of some other methoid.
Code:
#!/bin/bash

somestuff="$(ls / | wc -l)"

echo "this is it $somestuff" | tee ~/myLog

somemorestuff="$(ls /home/userx | wc -l)"

echo "this is  more stuff $somemorestuff" | tee >> ~/myLog
logfile
Code:
$ cat myLog
this is it 20
this is  more stuff 30
 
Old 04-01-2018, 06:17 AM   #6
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 8,745
Blog Entries: 5

Rep: Reputation: 2141Reputation: 2141Reputation: 2141Reputation: 2141Reputation: 2141Reputation: 2141Reputation: 2141Reputation: 2141Reputation: 2141Reputation: 2141Reputation: 2141
Quote:
Originally Posted by storms View Post
I already looked for solutions and one solution that i found and is NOT what i'm looking for is calling the script with exec > /tmp/myLog.log 2>&1.
what i want is an way to incorporate the write to log into the script!
Quote:
Originally Posted by storms View Post
because the way the script is done and the way is suppose to be used,
ex: i'm calling the script ./sdel.sh -r /home/cunha/LIXO , my teacher doesnt want us to call the log file when executing the script, the script is suppose to manage automatically a log file when used ./sdel.sh
is the second statement meant to clarify the first?
because i still don't understand; you want ALL the script's console output to be logged to a file, yes?
you aren't "calling" the log file at all. i just don't understand the statement.
why is "exec > /tmp/myLog.log 2>&1" not sufficient?
 
Old 04-01-2018, 08:28 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,503

Rep: Reputation: 1080Reputation: 1080Reputation: 1080Reputation: 1080Reputation: 1080Reputation: 1080Reputation: 1080Reputation: 1080
Just do this:
script before:
Code:
#!/bin/sh

first command here
script after:
Code:
#!/bin/sh

exec >>logfile 2>&1
set -vx

first command here
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
howto make a script for a log file cristi32 Linux - General 8 01-13-2015 06:59 AM
Script to check for text file is different and perform certain action depam Linux - Newbie 3 11-26-2012 09:54 PM
Shell script to monitor the log file & kill the process if log is not updating. milu_k Programming 5 07-19-2012 08:23 AM
How to write kernel log directly to a file without user action? 10110111 Linux - General 5 04-23-2009 08:29 PM
Firewall log file, how to make several different log files with IPTables? newtovanilla Linux - Newbie 5 11-28-2008 12:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration