I have a bash script that is used for auto updating. I want to log all output of this script and prepend a timestamp to each line. I'm not sure how to add this to what I already have.  LOGFILE="logdir/update.sh.$(date +%Y-%m-%d_%H:%M).log" exec 1&gt;$LOGFILE 2&gt;&amp;1   This redirects the output of my script to a file correctly, but there is (obviously) no date added to the line. How can I add this?   For instance, I want the logged output to look like this:  [2019-11-07 1:43:45 PM]: Ign http://security.debian.org jessie/updates InRelease  I'm not looking to prepend anything to each individual command my script has, I'm looking to prepend the timestamp and log each line any one command outputs globally throughout the script.
36412
I wish to prepend a timestamp to each line of output from a command. For example:  foo bar baz   would become  [2011-12-13 12:20:38] foo [2011-12-13 12:21:32] bar [2011-12-13 12:22:20] baz   ...where the time being prefixed is the time at which the line was printed. How can I achieve this?
36883
I use :  exec &gt;script.log 2&gt;&amp;1   in my script to redirect all output to a file. At the end of the script I would like to print a message to the screen. How do I stop the redirection?
98935
I have a sample script (credit goes to ):  #!/bin/bash cat &lt;(cat /etc/lsb-release)   I save this script as somename.sh in my home directory.  Now, I try to open this file in three different ways:  sh somename.sh  bash somename.sh  ./somename.sh   So, I have two questions:   Why are the outputs for the above commands different though they are running the same script?   sh produces a Syntax error bash outputs the desired result ./ gives a Permission Denied error  Why is it necessary to give the script executable permission only when running the script using ./ and not necessary in the other cases?   Thanks in advance!  EDIT: The first part might be similar to the  linked one but, I did have a second question too.
100570
I have here a bash script:  #!/bin/bash  TARGET_FILE=ping_result.txt  # declare the target ip addresses declare -a ips=("XXX.XXX.XXX.XXX" "YYY.YYY.YYY.YYY")  function print_and_log() {     echo "$1"     echo "$1" &gt;&gt; $TARGET_FILE 2&gt;&amp;1 }  # loop through all ip addresses for i in "${ips[@]}" do     print_and_log "----- Begin Pinging for $i --- "     print_and_log "command: ping -c10 $i"     print_and_log $(ping -c10 $i)     print_and_log "----- Pinging for $i end ----- " done   My aim is to print the same output into a file and into console. But when I disconnect my operation system from the network, then in the console, I see (for example for the first IP address):  ----- Begin Pinging for XXX.XXX.XXX.XXX ---  command: ping -c10 XXX.XXX.XXX.XXX connect: Network is not reachable  ----- Pinging for XXX.XXX.XXX.XXX end -----    But in the file, I do no see any message, that the Network is not reachable. Why? How can I change my bash script, that I also can see this message in my logging file.
117282
I am trying to append and prepend text to every line in a .txt file.  I want to prepend: I am a   I want to append:  128... [}  to every line.  Inside of a.txt:  fruit, like bike, like dino, like   Upon performing the following command:  $ cat a.txt|sed 'I am a ',' 128... [}'   it does not work how I want it to. I would really want it to say the following:  I am a fruit, like 128... [} I am a bike, like 128... [} I am a dino, like 128... [}
90845
I have a couple of scripts in my bash file, that should do certain tasks, say like ...   #!bin/bash yum -y update service restart nginx yum install atop cat /var/log/somelog.log &gt; /home/cron/php/script   It goes on and on, but the problem is that with each task taken, bash shows me the output. Like  service restart nginx for example, outputs some message. And I want to hide all these messages. What is generally the accepted way of achieving this? Because, I was about to redirect the STDOUT to /dev/null but, considering I have over 50 consecutive tasks to run, it would mean I have to /dev/null that much, which for some reason does not seem efficient to me.
82471
I've installed a version of Ubuntu Server and although I'm fairly still fresh with Ubuntu, I'm trying to create a bash file to run a program in the background. More importantly, I'm trying to get a file nameto display correct and create it before I start running the program (I like my debug stuff).  However, here is my bash script:    #!/bin/bash  PATH="/var/log/servers/Game/"  FILENAME="startup"  EXT=".log"  DATE=$(date +%d-%m-%Y)  NEWFILE=${PATH}${FILENAME}_${DATE}${EXT}   I have verified that the file does exist in the PATH directory. However, the issue is with the date command. When I run the script bash -x game.sh this is what is displays.   + PATH=/var/log/servers/Game/  + FILENAME=startup  + EXT=.log  ++ date +%d-%m-%Y  game.sh: line 5: date: command not found  + DATE=  + NEWFILE=/var/log/servers/Game/startup_.log  + echo /var/log/servers/Game/startup_.log   I don't understand why it would say the command doesn't work, when I can go to the main console and run date and get a valid response.  Any help is greatly appreciated!
84426
I am creating a simple backup script. I would like to have a message saying "You last backed up x days ago" on login. I can create a file when I do the backup "backup/.last_backup_timestamp" but what format should I use and how would I find the difference in days between then and now in bash?  Thank-you
78992
I have several files that I ftp each hour.  The receiving system needs to have some sort of identifier that they came from the same batch, so I would like to append a timestamp to the filename.  That alone is fairly easy, but since I want each file to have the same timestamp (so it can serve as the batch identifier), I cannot figure out how to do this.  So, I have:  file1.txt file2.txt file3.txt   And I want to have:  file1_20141110184303.txt file2_20141110184303.txt file3_20141110184303.txt   Any use of the date +%Y%m%d%H%M%S after the first use will obviously result in a different values in seconds, so I would like to have the first timestamp appended to the remaining files.
33496
I have a script that outputs text to stdout. I want to see all this output in my terminal, and at the same time I want to filter some lines and save them in a file. Example:  $ myscript Line A Line B Line C  $ myscript | grep -P 'A|C' &gt; out.file  $ cat out.file Line A Line C   I want to see output of first command in terminal, and save the output of the second command in a file. At the same time. I tried using tee, but with no result, or better, with .
84459
I was writing a bash script, and happened to updated the code (saved the script file to disk) while the script was waiting for some input in a while loop. After I returned to the terminal and continued with the previous invocation of the script, bash gave an error about the file syntax:  /home/aularon/bin/script: line 58: unexpected EOF while looking for matching `"' /home/aularon/bin/script: line 67: syntax error: unexpected end of file   So I tried doing the following:  1st: create a script, self-update.sh let's call it:  #!/bin/bash fname=$(mktemp) cat $0 | sed 's/BEFORE\./AFTER!./' &gt; $fname cp $fname $0 rm -f $fname echo 'String: BEFORE.';   What the script does is reading its code, changing the word 'BEFORE' into 'AFTER', then re-write itself with the new code.  2nd Run it:  chmod +x self-update.sh ./self-update.sh   3rd wonder...  aularon@aularon-laptop:~$ ./self-update.sh  String: AFTER!.   Now, I wouldn't have guessed that on the same invocation it would output AFTER!, on the second run for sure, but not on the first one.  So my question is: is it intentional (by design)? or it is because of the way bash runs the script? Line by line or command by command. Is there any good use of such behaviour? Any example of it?    Edit: I tried reformatting the file to put all command in one line, it does not work now:  #!/bin/bash fname=$(mktemp);cat $0 | sed 's/BEFORE\./AFTER!./' &gt; $fname;cp $fname $0;rm -f $fname;echo 'String: BEFORE.';   Output:   aularon@aularon-laptop:~$ ./self-update.sh #First invocation String: BEFORE. aularon@aularon-laptop:~$ ./self-update.sh #Second invocation String: AFTER!.   While when moving the echo string to the next line, separating it from the rewriting (cp) call:  #!/bin/bash fname=$(mktemp);cat $0 | sed 's/BEFORE\./AFTER!./' &gt; $fname;cp $fname $0;rm -f $fname; echo 'String: BEFORE.';   And now it works again:  aularon@aularon-laptop:~$ ./self-update.sh  String: AFTER!.
2306
I am writing a bash script to look for a file if it doesn't exist then create it and append this to it:  Host localhost     ForwardAgent yes   So "line then new line 'tab' then text" I think its a sensitive format.  I know you can do this:  cat temp.txt &gt;&gt; data.txt   But it seems weird since its two lines. Is there a way to append that in this format:  echo "hello" &gt;&gt; greetings.txt
4052
I have a bash script and I want to capture a portion of the script with a set -x and redirect that output to a file.   I can see what the ENVFILE is setting but its going to std out.   Is there a way I can capture the output of the set -x into a file instead of std out ?     #!/bin/sh ...a bunch of stuff....  if [ -e ${ENVFILE} ]; then set -x . ${ENVFILE} set +x fi # ! -e PROJOB ... a bunch of stuff ....
110737
I want to get the result of time command into a text file but it's not working it only put blank space in the text file.  I already tried this commands,  A-  $ x=`time` $ echo $x &gt; log.txt $ cat log.txt  $   B-   $ time &gt; log.txt  real    0m0.000s user    0m0.000s sys     0m0.000s $ cat log.txt  $   C-  $ time &gt; log.txt 2&gt;&amp;1  real    0m0.000s user    0m0.000s sys     0m0.000s  $ cat log.txt  $    What I really want is this.   $ time &gt; log.txt $ cat log.txt  real    0m0.000s user    0m0.000s sys     0m0.000s
36422
How to redirect standard output to multiple log files? The following does not work:  some_command 1&gt; output_log_1 output_log_2 2&gt;&amp;1
10649
How would I get all outputs of systemd managed python2 script instantly into journal, just like it appears in terminal  I'm on centos 7, but seems to be same on arch  according to systemd manual, stdout and stderr should automatically go to journal  systemd config  #DefaultStandardOutput=journal #DefaultStandardError=inherit   Here is my unit /usr/lib/systemd/system/tick.service  [Unit] Description=Tick  [Service] Type=simple ExecStart=/home/stack/tick   script  #!/usr/bin/python import time import sys  print "Tick" sys.stderr.write('First error\n') time.sleep(10) sys.stdout.write("Tick\n") sys.stderr.write('Second error\n') time.sleep(10)   and here is the output from journal, notice the time and order  Oct 30 14:30:41 controller systemd[1]: Starting Tick... Oct 30 14:30:41 controller systemd[1]: Started Tick. Oct 30 14:30:41 controller tick[5020]: First error Oct 30 14:30:51 controller tick[5020]: Second error Oct 30 14:31:01 controller tick[5020]: Tick Oct 30 14:31:01 controller tick[5020]: Tick   With python3 even stderr is delayed until process ends  I tried similar c program, and seems to be same  but this does not seem to affect bash script  #!/bin/bash echo "Tick" echo "First error" 1&gt;&amp;2 sleep 10 echo "Tick" echo "Second error" 1&gt;&amp;2 sleep 10   journal  Oct 30 14:41:36 controller systemd[1]: Starting Tick... Oct 30 14:41:36 controller systemd[1]: Started Tick. Oct 30 14:41:36 controller tick[5084]: Tick Oct 30 14:41:36 controller tick[5084]: First error Oct 30 14:41:46 controller tick[5084]: Tick Oct 30 14:41:46 controller tick[5084]: Second error
40191
Is it possible to redirect all of the output of a Bourne shell script to somewhere, but with shell commands inside the script itself? Redirecting the output of a single command is easy, but I want something more like this: #!/bin/sh if [ ! -t 0 ]; then     # redirect all of my output to a file here fi  # rest of script...  Meaning: if the script is run non-interactively (for example, cron), save off the output of everything to a file.  If run interactively from a shell, let the output go to stdout as usual. I want to do this for a script normally run by the FreeBSD periodic utility.  It's part of the daily run, which I don't normally care to see every day in email, so I don't have it sent.  However, if something inside this one particular script fails, that's important to me and I'd like to be able to capture and email the output of this one part of the daily jobs. Update: Joshua's answer is spot-on, but I also wanted to save and restore stdout and stderr around the entire script, which is done like this: # save stdout and stderr to file  # descriptors 3 and 4,  # then redirect them to &quot;foo&quot; exec 3&gt;&amp;1 4&gt;&amp;2 &gt;foo 2&gt;&amp;1  # ...  # restore stdout and stderr exec 1&gt;&amp;3 2&gt;&amp;4