Output shell script result to file like terminal screen - linux

I am a beginner in shell scripting and this is my sample script:
#!/bin/ksh
echo "start script"
ls
echo "end script"
I run it by this command: ./runScript.ksh > outPutFile.txt
and got the output inside file:
start script
file1.txt
file2.txt
file3.txt
end script
I just want to ask if it is possible to have this output on the file? Like what is shown if the commands are executed on the terminal?:
user#serverName:/parentPath/childPath/>echo "start script"
user#serverName:/parentPath/childPath/>ls
file1.txt
file2.txt
file3.txt
user#serverName:/parentPath/childPath/>echo "end script"
Thank you so much in advance. I will really appreciate any help.

Use the 'script' command. Any command issued after the 'scipt' command will be captured in the specified file (or default file typescript).
**Example:**
$ script my_output.txt
Script started, output file is my_output.txt
$ uname -r
16.1.0
$ date
Wed Mar 8 12:55:29 IST 2017
$ exit
Script done, output file is my_output.txt
$ cat my_output.txt
Script started on Wed Mar 8 12:55:19 2017
$ uname -r
16.1.0
$ date
Wed Mar 8 12:55:29 IST 2017
$ exit
Script done on Wed Mar 8 12:55:32 2017
$
man script
SCRIPT(1) BSD General Commands Manual SCRIPT(1)
NAME
script -- make typescript of terminal session
SYNOPSIS
script [-adkpqr] [-F pipe] [-t time] [file [command ...]]
DESCRIPTION
The script utility makes a typescript of everything printed on your terminal. It is useful for students who need a hardcopy record of an interactive session
as proof of an assignment, as the typescript file can be printed out later with lpr(1).
If the argument file is given, script saves all dialogue in file. If no file name is given, the typescript is saved in the file typescript.

Related

Passing arguments to at -f script

I'm trying to schedule a bash script using at command on Linux.
at 22:20 -f /path/to/script.sh
Issuing the command above works just fine. However, the script requires some parameters. Adding Params behind the script path returns an error message:
at 22:20 -f /path/to/script.sh /arg/one argtwo argthree
syntax error. Last token seen: /
Yes, the first parameter passed to the script is another (absolute) path. My guess is, that at doesn't treat my script as a script but rather as a file, as at -help implies.
How can I work around that and add params to the script?
Specify the command to run on stdin, e.g. via a here string:
at 22:20 <<< "/path/to/script.sh /arg/one argtwo argthree"
Your command does not try to run /path/to/script.sh at a certain time. Instead, it reads and copies all the commands from /path/to/script.sh and runs those later. Since you're not invoking the script itself, it doesn't make sense to talk about arguments.
You're correct that it's taking just a filename, not a command. You should just run at 22:20, and then as input to that command, give the command you want run at 22:20 (i.e. /path/to/script.sh /arg/one argtwo argthree), and then Control-D on a separate line to mark the end of input. It should look a little like this ($ is my prompt):
$ at 22:20
/path/to/script.sh /arg/one argtwo argthree
job 11 at Thu May 27 22:20:00 2021
$
(Note that the "job 11 ..." is a confirmation message at printed.)
You can give any command as input without the -f:
at 22:20
/path/to/script.sh /arg/one argtwo argthree
^d

"Ambiguous output redirect" trying to send both stdout and stderr to mailx from a command sent to at

I have a bash script called test.sh which, for the sake of simplicity, prints one line to stdout and one line to stderr.
test.sh:
#!/bin/bash
echo "this is to stdout"
echo "this is to stderr" 1>&2
I want to run the script test.sh at 7:00 PM, but only if certain conditions are met. To this end, I have another bash script called schedule.sh, which checks some stuff and then submits the command to at to be run later.
I want the output of test.sh (both stdout and stderr) to be sent to me in an email. I use mailx to do this so I can get a nice subject name.
Furthermore, I want at to shut up. No output from at because it always sends me ugly emails (no subject line) if at produces any output.
schedule.sh:
#!/bin/bash
my_email="me#example.com" # Email is a variable
# Check some stuff, exit if certain conditions not met
echo "~/test.sh 2>&1 | mailx -s\"Cool title\" $my_email" | at 7:00 PM &> /dev/null
What's interesting is that when I run schedule.sh from cron (which runs the script with sh), it works perfectly. However, when I manually run schedule.sh from the terminal (NB: I'm using tcsh), at (not mailx) sends me an email saying
Ambiguous output redirect.
I'm not sure why the shell I run schedule.sh from makes a difference, when schedule.sh is a bash script.
Here is my thinking in looking at schedule.sh. Everything within the quotation marks "~/test.sh 2>&1 | mailx -s\"Cool title\" me#email.com" should be an argument to at, and at runs that argument as a command using sh. The redirection 2>&1 | is in the style of sh for this reason.
When I remove 2>&1 and only pipe the stdout of test.sh to mailx, it does work; however, I receive 2 emails: one with stdout from mailx and another from stderr from at.
What gives? How can I make this work regardless of the shell I'm calling it from?
Thanks.
edit:
uname -o says my OS is GNU/Linux
Here is uname -a if it helps:
Linux [hostname censored] 2.6.9-89.ELlargesmp #1 SMP Mon Jun 22 12:46:58 EDT 2009 x86_64 x86_64 x86_64 GNU/Linux
When I check the at contents using at -c, here's what I see:
#!/bin/sh
# atrun uid=xxxxx gid=xxxxx
# mail username 0
# ...
SHELL=/bin/tcsh; export SHELL
# ...
${SHELL:-/bin/sh} << `(dd if=/dev/urandom count=200 bs=1 2>/dev/null|LC_ALL=C tr -d -c '[:alnum:]')`
~/test.sh 2>&1 | mailx -s"Cool title" me#example.com
I'm having a hard time understanding the second to last line... is this going to execute using $SHELL or /bin/sh?
The command executed via at is:
~/test.sh 2>&1 | mailx -s\"Cool title\" $my_email
The behavior of at command varies from one system to another. On Linux, the command is executed using /bin/sh. In fact, on my system (Linux Mint 14), it prints a warning message:
$ echo 'printenv > at.env' | at 19:24
warning: commands will be executed using /bin/sh
On Solaris, the command is executed by the shell specified by the current value of the $SHELL environment variable. Using an account where my default shell is /bin/tcsh on Solaris 9, I get:
% echo 'printenv > at.env' | at 19:25
commands will be executed using /bin/tcsh
job 1397874300.a at Fri Apr 18 19:25:00 2014
% echo 'printenv > at.env' | env SHELL=/bin/sh at 19:28
commands will be executed using /bin/sh
job 1397874480.a at Fri Apr 18 19:28:00 2014
Given that at's behavior is inconsistent (and frankly confusing), I suggest having it execute just a single command, with any I/O redirection being performed inside that command. That's the best way to ensure that the command will be executed correctly regardless of which shell is used to execute it.
For example (untested code follows):
echo '#!/bin/bash' > tmp.bash
echo "~/test.sh 2>&1 | mailx -s\"Cool title\" $my_email" >> tmp.bash
chmod +x tmp.bash
echo "./tmp.bash" | at 7:00 PM

Copy all typed command in a linux console & their result to a file

I'm trying to make a script to automatically install programs and configure them on my Fedora 19 linux distribution.
To create it, I made a VM and I'm typing all the command manually in my "Terminal" application.
I'd like to be able to log all what I've typed and all the output (stdin & stderr & stdout if I understood it well) so I can use this log to make my script.
Is there a way to do this ?
You can use the script command to record your session:
$ script session.txt
Script started, file is session.txt
$ ls
session.txt
$ exit
Script done, file is session.txt
$ cat session.txt
Script started on Wed 31 Jul 2013 07:36:40 AM CEST
$ ls
session.txt
$ exit
Script done on Wed 31 Jul 2013 07:36:42 AM CEST

user-data (cloud-init) script not executing on EC2

my user-data script
#!
set -e -x
echo `whoami`
su root
yum update -y
touch ~/PLEASE_WORK.txt
which is fed in from the command:
ec2-run-instances ami-05355a6c -n 1 -g mongo-group -k mykey -f myscript.sh -t t1.micro -z us-east-1a
but when I check the file /var/log/cloud-init.log, the tail -n 5 is:
[CLOUDINIT] 2013-07-22 16:02:29,566 - cloud-init-cfg[INFO]: cloud-init-cfg ['runcmd']
[CLOUDINIT] 2013-07-22 16:02:29,583 - __init__.py[DEBUG]: restored from cache type DataSourceEc2
[CLOUDINIT] 2013-07-22 16:02:29,686 - cloud-init-cfg[DEBUG]: handling runcmd with freq=None and args=[]
[CLOUDINIT] 2013-07-22 16:02:33,691 - cloud-init-run-module[INFO]: cloud-init-run-module ['once-per-instance', 'user-scripts', 'execute', 'run-parts', '/var/lib/cloud/data/scripts']
[CLOUDINIT] 2013-07-22 16:02:33,699 - __init__.py[DEBUG]: restored from cache type DataSourceEc2
I've also verified that curl http://169.254.169.254/latest/user-data returns my file as intended.
and no other errors or the output of my script happens. how do I get the user-data scrip to execute on boot up correctly?
Actually, cloud-init allows a single shell script as an input (though you may want to use a MIME archive for more complex setups).
The problem with the OP's script is that the first line is incorrect. You should use something like this:
#!/bin/sh
The reason for this is that, while cloud-init uses #! to recognize a user script, the operating system needs a complete shebang line in order to execute the script.
So what's happening in the OP's case is that cloud-init behaves correctly (i.e. it downloads and tries to run the script) but the operating system is unable to actually execute it.
See: Shebang (Unix) on Wikipedia
Cloud-init does not accept plain bash scripts, just like that. It's a beast that eats YAML file that defines your instance (packages, ssh keys and other stuff).
Using MIME you can also send arbitrary shell scripts, but you have to MIME-encode them.
$ cat my-boothook.txt
#!/bin/sh
echo "Hello World!"
echo "This will run as soon as possible in the boot sequence"
$ cat my-user-script.txt
#!/usr/bin/perl
print "This is a user script (rc.local)\n"
$ cat my-include.txt
# these urls will be read pulled in if they were part of user-data
# comments are allowed. The format is one url per line
http://www.ubuntu.com/robots.txt
http://www.w3schools.com/html/lastpage.htm
$ cat my-upstart-job.txt
description "a test upstart job"
start on stopped rc RUNLEVEL=[2345]
console output
task
script
echo "====BEGIN======="
echo "HELLO From an Upstart Job"
echo "=====END========"
end script
$ cat my-cloudconfig.txt
#cloud-config
ssh_import_id: [smoser]
apt_sources:
- source: "ppa:smoser/ppa"
$ ls
my-boothook.txt my-include.txt my-user-script.txt
my-cloudconfig.txt my-upstart-job.txt
$ write-mime-multipart --output=combined-userdata.txt \
my-boothook.txt:text/cloud-boothook \
my-include.txt:text/x-include-url \
my-upstart-job.txt:text/upstart-job \
my-user-script.txt:text/x-shellscript \
my-cloudconfig.txt
$ ls -l combined-userdata.txt
-rw-r--r-- 1 smoser smoser 1782 2010-07-01 16:08 combined-userdata.txt
The combined-userdata.txt is the file you want to paste there.
More info here:
https://help.ubuntu.com/community/CloudInit
Also note, this highly depends on the image you are using. But you say it is really cloud-init based image, so this applies. There are other cloud initiators which are not named cloud-init - then it could be different.
This is a couple years old now, but for others benefit I had the same issue, and it turned out that cloud-init was running twice, from inside /etc/rc3.d . Deleting these files inside the folder allowed the userdata to run correctly:
lrwxrwxrwx 1 root root 22 Jun 5 02:49 S-1cloud-config -> ../init.d/cloud-config
lrwxrwxrwx 1 root root 20 Jun 5 02:49 S-1cloud-init -> ../init.d/cloud-init
lrwxrwxrwx 1 root root 26 Jun 5 02:49 S-1cloud-init-local -> ../init.d/cloud-init-local
The problem is with cloud-init not allowing the user script to run on the next start-up.
First remove the cloud-init artifacts by executing:
rm /var/lib/cloud/instances/*/sem/config_scripts_user
And then your userdata must look like this:
#!/bin/bash
echo "hello!"
And then start you instance. It now works (tested).

shell script to capture keyboard activities

How to capture all keyboard strokes using shell script .Is there any command that is related to keyboard activities.
If you would like to log all input and output, you can use the script command.
$ script transcript.txt
Script started, file is transcript.txt
$ echo 'Hello, world!'
Hello, world!
$ exit
Script done, file is transcript.txt
$ cat transcript.txt
Script started on Thu 09 Sep 2010 03:06:56 PM EDT
$ echo 'Hello, world!'
Hello, world!
$ exit
Script done on Thu 09 Sep 2010 03:07:06 PM EDT
Check out the trap command.
For example, type in your console :
trap "echo \"Arrrrggghhhh\"" INT
Now press Ctrl + C - fun fun :)

Resources