Issue exporting bash shell output to text file - bash

Below is my code:
#!/bin/bash
echo 1. date
date
echo -e "\n"
echo 2. hostname
hostname
echo -e "\n"
echo 3. arch
arch
echo -e "\n"
echo 4. uname -a
uname -a
echo -e "\n"
echo 5.uptime
uptime
echo -e "\n"
echo 6. whoami
whoami
echo -e "\n"
echo 7. who
who
echo -e "\n"
echo 8. last
last
echo -e "\n"
echo 9. finger
finger
echo -e "\n"
echo 10. w
w
echo -e "\n"
echo 11. top
top -n 1
echo -e "\n"
echo 12. history
tail -100 ${HOME}/.bash_history
echo -e "\n"
echo End of Script!
when I run it works fine and displays all the code on screen, however when I save to txt file using ./keithbrazill.sh > keithbrazill.txt, it does not show all the text output and only part output. Any advice? I tried a few different variations of output command such as &>, >> etc.

Here's an easier way to reproduce the problem:
echo "Hello"; whoami # Shows output from both commands
echo "World"; top -n 1 # Shows output only from top
This happens because top is a ncurses program that outputs terminal control sequences to draw itself fullscreen, overwriting anything already on screen. While all the data does end up in the file, it'll show a lot of gibberish in a text editor, and if you cat it, the terminal sequences will overwrite the output of other commands.
You can use top -b -n 1 to instead get plaintext output.

Related

Read from .txt file containing executable commands to be executed w/ output of commands executed sent to another file

When I run my script, the .txt file is read, the executable commands are assigned to $eggs, then to execute the commands and redirect the output to a file I use echo $eggs>>eggsfile.txt but when I cat the file, I just see all the commands and not the execution output of those commands.
echo "Hi, $USER"
cd ~/mydata
echo -e "Please enter the name of commands file:/s"
read fname
if [ -z "$fname" ]
then
exit
fi
terminal=`tty`
exec < $fname #exec destroys current shell, opens up new shell process where FD0 (STDIN) input comes from file fname
count=1
while read line
do
echo $count.$line #count line numbers
count=`expr $count + 1`; eggs="${line#[[:digit:]]*}";
touch ~/mydata/eggsfile.txt; echo $eggs>>eggsfile.txt; echo "Reading eggsfile contents: $(cat eggsfile.txt)"
done
exec < $terminal
If you just want to execute the commands, and log the command name before each command, you can use 'sh -x'. You will get '+ command' before each command.
sh -x commands
+pwd
/home/user
+ date
Sat Apr 4 21:15:03 IDT 2020
If you want to build you own (custom formatting, etc), you will have to force execution of each command. Something like:
cd ~/mydata
count=0
while read line ; do
count=$((count+1))
echo "$count.$line"
eggs="${line#[[:digit:]]*}"
echo "$eggs" >> eggsfile.txt
# Execute the line.
($line) >> eggsfile.txt
done < $fname
Note that this approach uses local redirection for the while loop, avoiding having to revert the input back to the terminal.

sed command garbled for bash 3.0

I have gone through other threads Sed command garbled didnt work
but it didn't helped me
flag=1
echo "enter the folder into which you want to capture"
read logs
mkdir $logs
path=/user/gur40139/shell/angel
for i in $path/*.tra*
do
value=$( grep -ic \*= $i )
if [ $value -ge $flag ]
then
name=`basename $i .tra\*`
echo -e "count is $value\n" >> $path/$logs/log_"$name".txt
sed -n '/\*=/ {n;p}' $i|sed 2n\;G >> $path/$logs/log_"$name".txt
fi
done
echo -e "\nDone\n"
Error:
sed: command garbled: /\*=/ {n;p}
Additional Note: This code is working properly on bash 4.1 version but I want to test it in 3.0, There many options which are not even working like sed --version.
sed -n '/\*=/ {n;p;}' ...
you need to terminate the line after the p so a ; or a new line. Your code will certainly work on recent GNU sed but not on posix version

how to create and send output directly to printer using do shell script

I am trying to get a shell script that works in the sh shell to work as an applescript do shell script command. The script simply takes a series of elp2 commands and pipes them to lpr for printing. This works when typed directly into the shell, but when run in applescript it does not print. The printer is receiving data but it is not formated correctly so nothing prints. I believe the issue is with the way the quotes are escaped. Here is the code:
do shell script "{ echo N
echo OD10
echo q812
echo Q1218,24
echo D15
echo ZT
echo A70,40,0,5,3,3,N,\\'F\\'
echo A610,70,0,3,1,1,N,\\'U.S.\\'
echo A590,95,0,3,1,1,N,\\'POSTAGE\\'
echo A580,120,0,3,1,1,N,\\'REQUIRED\\'
echo A43,240,0,4,2,1,N,\\'USPS FIRST-CLASS MAIL\\'
echo A43,300,0,3,1,1,N,\\'Name\\'
echo A43,325,0,3,1,1,N,\\'street\\'
echo A43,350,0,3,1,1,N,\\'city, VA 12345\\'
echo A43,400,0,3,1,1,N,\\'ADDRESS SERVICE REQUESTED\\'
echo A140,490,0,4,1,1,N,\\'ship to\\'
echo A140,520,0,4,1,1,N,\\'company\\'
echo A140,550,0,4,1,1,N,\\'address\\'
echo A140,580,0,4,1,1,N,\\'city, state, 12345\\'
echo A140,610,0,4,1,1,N,\\'country\\'
echo A140,640,0,4,1,1,N,\\'\\'
echo LO10,10,760,4
echo LO10,210,760,2
echo LO10,275,760,2
echo LO10,750,760,10
echo LO10,1050,760,10
echo LO10,1185,760,4
echo LO10,10,4,1175
echo LO770,10,4,1175
echo LO210,10,2,200
echo LO540,45,200,2
echo LO540,45,2,125
echo LO540,170,200,2
echo LO740,45,2,125
echo P1
echo N;} | lpr -P Label -o raw"
One way you can check how to write something in applescript is to write the normal unescaped text into a text file. Then you read the file into applescript. What you see in the result field is how you should write it. You can then copy/paste the result into your applescript.
For example, if I make a text file on my desktop called myText.txt with this inside...
echo A70,40,0,5,3,3,N,\'F\'
echo A610,70,0,3,1,1,N,\'U.S.\'
Then I use this applescript...
set f to (path to desktop as text) & "myText.txt"
read file f
My result is...
"echo A70,40,0,5,3,3,N,\\'F\\'
echo A610,70,0,3,1,1,N,\\'U.S.\\'"
So give that technique a try with your entire code. Good luck.

Shell script output to Console and Log on same line?

Is it possible for me to cut these two lines of shell script into one line.
My two lines of code echo the same string but one goes into the Console and the other into a log file.
echo "Starting scriptr" `date '+%T'` >> script.log
echo "Starting script" `date '+%T'`
Thanks
Use tee:
echo "Starting scriptr" `date '+%T'` | tee script.log
In order to append to the log file, say tee -a
Quoting from man tee:
tee - read from standard input and write to standard output and files

Shell scripting, store command output in variable and preserve formatting [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Capturing multiple line output to a bash variable
I have what is probably a basic scripting question, but I haven't been able to find an answer anywhere that I've looked.
I have an awk script that processes a file, and spits out a list of disabled systems. When I call it manually from the command line, I get formatted output back:
$awk -f awkscript datafile
The following notifications are currently disabled:
host: bar
host: foo
I am writing a wrapper script to call from my crontab, which will run the awk script, determine if there is any output, and email me if there is. It looks like (simplified):
BODY=`awk -f awkscript datafile`
if [ -n "$BODY" ]
then
echo $BODY | mailx -s "Disabled notifications" me#mail.address
else
echo "Nothing is disabled"
fi
When run this way, and confirmed by adding an echo $BODY into the script, the output is stripped of the formatting (newlines are mainly what I'm concerned with), so I get output that looks like:
The following notitifications are currently disabled: host: bar host: foo
I'm trying to figure out how to preserve the formatting that is present if I run the command manually.
Things I've tried so far:
echo -e `cat datafile | awkscript` > /tmp/tmpfile
echo -e /tmp/tmpfile
I tried this because on my system (Solaris 5.10), using echo without the -e ignores standard escape sequences like \n . Didn't work. I checked the tmpfile, and it doesn't have any formatting in it, so the problem is happening when storing the output, not when printing it out.
BODY="$(awk -f awkscript datafile)"
echo -e "$BODY"
I tried this because everything I could find, including some other questions here on stackoverflow said that the problem was that the shell would replace whitespace codes with spaces if it wasn't quoted. Didn't work.
I've tried using printf instead of echo, using $(command) instead of `command`, and using a tempfile instead of a variable to store the output, but nothing seems to retain the formatting.
What am I missing, or is there another way to do this which avoids this problem all together?
BODY=`awk -f awkscript datafile`
if [ -n "$BODY" ]
then echo "$BODY" | mailx -s "Disabled notifications" me#mail.address
else echo "Nothing is disabled"
fi
Note the double quotes in the echo.
You can simplify this version, too:
echo -e `cat datafile | awkscript` > /tmp/tmpfile
echo -e /tmp/tmpfile
to just:
tmpfile=/tmp/tmpfile.$$
awkscript > $tmpfile
if [ -s $tmpfile ]
then mailx -s "Disabled notifications" me#mail.address < $tmpfile
else echo "Nothing is disabled"
fi
Backquotes are useful (but better written as $(cmd args)) but do not have to be used everywhere.
Using quotes should work, and does for me:
$ cat test.sh
#!/bin/bash -e
BODY=`cat test.in`
if [ -n "$BODY" ]; then
echo "$BODY" | mailx -s "test" username
else
echo "Nothing"
fi
$ cat test.in
The following notifications are currently disabled:
host: bar
host: foo
$ ./test.sh
$
And this sends me a properly formatted email.

Resources