2

I am trying to use a shell program that I have been left to get running. The program starts as shown below.

#!/usr/bin/env bash
exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3
exec 1>$1/logs/$(date '+%Y%m%d_%H%M%S')_start.log 2>&1

The program is a .sh with the input ($1) being the output folder such that it is looking in /output/logs..., the program fails on the exec line, this is my first time using bash and shell scripting but I think this line essentially redirects all the standard outputs to the log file?

The program error say

cannot create /output/logs/20230203_12345_start.log: directory non-existent

should this line also create the log file if it is non existent? I don't see how you could create the .log file first as otherwise you would get the seconds part of the title wrong?

1
  • As @Verpous pointed out the error message is about a missing directory, not a missing file, but to address "I don't see how you could create the .log file first as otherwise you would get the seconds part of the title wrong?" - if you did need to, you could create the file first by defining a variable to hold the file name, e.g. out="$1/logs/$(date '+%Y%m%d_%H%M%S')_start.log"; >"$out"; exec 1>"$out" 2>&1 Commented Feb 7, 2023 at 13:19

1 Answer 1

4

It's not complaining about the file name, it says directory non-existent. I/O redirection can't make directories. Create it first with mkdir -p "$1"/logs, then your line should work.

Sign up to request clarification or add additional context in comments.

1 Comment

Always mkdir -p "$1"/logs || exit 1 or similar to make sure you did create the directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.