From 5d78a62a0d3365b131ff00b7019b25f3a417d24f Mon Sep 17 00:00:00 2001 From: Jack-Benny Persson Date: Thu, 26 Dec 2013 21:13:20 +0100 Subject: [PATCH] Testing out different forms of redirections --- Misc/all_normal.txt | 3 +++ Misc/errorlog | 1 + Misc/file_desc3 | 1 + Misc/redirections.sh | 54 ++++++++++++++++++++++++++++++++++++++ Misc/redirections_reset.sh | 16 +++++++++++ Misc/reset1 | 1 + Misc/reset_test | 1 + 7 files changed, 77 insertions(+) create mode 100644 Misc/all_normal.txt create mode 100644 Misc/errorlog create mode 100644 Misc/file_desc3 create mode 100755 Misc/redirections.sh create mode 100755 Misc/redirections_reset.sh create mode 100644 Misc/reset1 create mode 100644 Misc/reset_test diff --git a/Misc/all_normal.txt b/Misc/all_normal.txt new file mode 100644 index 0000000..444eb36 --- /dev/null +++ b/Misc/all_normal.txt @@ -0,0 +1,3 @@ +Everything here will be redirected to a file called all_normal +Even this line +And this line too diff --git a/Misc/errorlog b/Misc/errorlog new file mode 100644 index 0000000..166b2fe --- /dev/null +++ b/Misc/errorlog @@ -0,0 +1 @@ +This line should go to the errorlog diff --git a/Misc/file_desc3 b/Misc/file_desc3 new file mode 100644 index 0000000..5efa28d --- /dev/null +++ b/Misc/file_desc3 @@ -0,0 +1 @@ +This will go to file_desc3 file diff --git a/Misc/redirections.sh b/Misc/redirections.sh new file mode 100755 index 0000000..6ecd05b --- /dev/null +++ b/Misc/redirections.sh @@ -0,0 +1,54 @@ +#!/bin/bash + +# Jack-Benny Persson +# LX13 +# Testing diffrent forms of redirections + +echo "This is an error message" >&2 # To redirect to a file decriptor it's must + # be preceded by a & sign + +echo "This is also an error message" > /dev/stderr # The same as above + +echo "This is a normal message" >&1 # To STDOUT instead of STDERR + +exec 1>all_normal.txt # All output below will go to a file + # Very practical for logfiles and such + +echo "Everything here will be redirected to a file called all_normal" +echo "Even this line" +echo "And this line too" + + +exec >&2 # All output below will go to STDERR + # Very practical for important errors and such + +echo "But everything from here will go to STDERR" +echo "All the errors..." + + +exec 2>errorlog # All errors below should be redirected to a logfile + # called errorlog + +echo "This line should go to the errorlog" >&2 + + +exec 0< errorlog # The same can be done for input + # Nice for processing logs etc + +#cat # This will read from STDIN, which is now errorlog, and output will be put + # on STDOUT... epic stuff :) + +# This will also read from STDIN, which is now errorlog, and put it's output on +# STDOUT togheter with line number +Count=1 +while read Line; do + echo "Errorlog: $Line" + ((Count++)) +done + +# One can also create new file descriptors + +exec 3>file_desc3 +echo "This will go to file_desc3 file" >&3 + +exit 0 diff --git a/Misc/redirections_reset.sh b/Misc/redirections_reset.sh new file mode 100755 index 0000000..6e01bea --- /dev/null +++ b/Misc/redirections_reset.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +# Jack-Benny Persson +# LX13 +# Testing to reset a permanent redirection + +exec 3>&1 +exec 1>reset_test + +echo "This line should go to the reset_test file" + +exec 1>&3 + +echo "And this should be outputted on STDOUD as normal" + +exit 0 diff --git a/Misc/reset1 b/Misc/reset1 new file mode 100644 index 0000000..8b0bb66 --- /dev/null +++ b/Misc/reset1 @@ -0,0 +1 @@ +This should go to STDOUT as usual diff --git a/Misc/reset_test b/Misc/reset_test new file mode 100644 index 0000000..4c42ce4 --- /dev/null +++ b/Misc/reset_test @@ -0,0 +1 @@ +This line should go to the reset_test file