How to print the previous linux command's output? - linux

The problem is:
After user enter a linux command.
How can I get the output of the first command using another command?
Note: we cannot redirect output of first command to somewhere.

Using history expansion
$ date -d "12:00"
Thu Sep 19 12:00:00 EDT 2013
$ d=$(!!)
$ echo $d
Thu Sep 19 12:00:00 EDT 2013

Related

How to have bash script's log file be auto-created after removal

I have a bash script file date.sh:
#!/bin/bash
while true
do
sleep 1
echo "date------ "$(date)
done
I run it
$ ./date.sh >> date.log 2>&1 &
I can see a date.log in there and be updating, but after I removed it, it won't be auto created, even I manually recreated it, the file won't update , I want date.log be auto created and update after it be removed.
In this code, date.log is opened just once:
./date.sh >> date.log 2>&1 &
If you want date.log to recreate itself if missing, you need to re-open each time that you write to it:
#!/bin/sh
while true; do
sleep 1
echo "date------ $(date)" >>date.log
done
Because the redirection >>date.log is inside the loop, the file is opened (and closed) with each loop. That is what is needed to re-create the file.
You can then run it:
./date.sh &
Now, if you delete or rename date.log, a new file called date.log will be created and written to.
Note that re-opening and re-closing the file with each loop is less efficient. Unless you want the re-create-itself feature, it is faster to open and close just once.
Example
This shows that we can delete date.log while the script is running in the background and the file will soon be recreated and appended to:
$ ./date.sh &
[1] 15678
$ cat date.log
date------ Sat Jul 30 00:51:28 PDT 2016
date------ Sat Jul 30 00:51:29 PDT 2016
date------ Sat Jul 30 00:51:30 PDT 2016
date------ Sat Jul 30 00:51:31 PDT 2016
$ rm -f date.log
$ cat date.log
date------ Sat Jul 30 00:51:38 PDT 2016
date------ Sat Jul 30 00:51:39 PDT 2016
date------ Sat Jul 30 00:51:40 PDT 2016
date------ Sat Jul 30 00:51:41 PDT 2016
What if date.sh cannot be modified
Suppose that date.sh is owned by others and we cannot modify it. In that case:
./date.sh | awk -v f=date.log '{print>>f; close(f)}' &
awk loops through each line of input and, for each line, it opens date.log, appends to it, and closes it.
Alternatively, if for some reason we wanted to stick with pure shell:
./date.sh | while IFS= read -r line; do printf "%s\n" "$line" >>date.log; done &

Salt cmd.run to NOT show the server name

When running a command from a salt-master linux box I always get something similar to the following result:
[root#salt-master ~]# salt 'target-server' cmd.run 'date'
target-server:
Fri Jul 24 22:41:44 UTC 2015
What can I do to get only the result of the command and NOT the targeted server too?
[root#salt-master ~]# salt 'target-server' cmd.run 'date' --SOMETHING I HAVE TO DO---
Fri Jul 24 22:41:44 UTC 2015
You can use Salt's JSON output format together with the command line utility jq to extract the values you need. On CentOS you'll need to install the package jq to use it.
The command line
salt 'target-server' cmd.run 'date' --out json | jq -r '.[]'
will output
Sun Jul 26 15:17:40 UTC 2015
(the -r option prevents double quotes around the output).
There are several other output formats available for Salt.

Bash: Program next execution of current script using 'at'

I want to execute a script and make it schedule the next execution. A sample would be:
#!/bin/bash
TMP=/tmp/text.txt
SCRIPT=$(readlink -f $0)
date >>$TMP
at -f $SCRIPT now + 1 minutes >>$TMP 2>&1
echo -e "\n" >>$TMP
A sample execution would do as follows:
First execution OK. Schedules to next minute
Second execution writes OK but doesn't schedule
Resulting output would be:
tue mar 5 14:34:01 CET 2013
job 15 at 2013-03-05 14:35
tue mar 5 14:35:00 CET 2013
job 16 at 2013-03-05 14:36
[now at 2013-03-05 14:38]
atq outputs nothing and I don't see any /var/at/jobs (In fact, ls /var/at* outputs nothing. There is no message in any user in /var/mail/. I'm trying on a CentOS release 5.6 x86_64
Anyone has any hint as to what may be happening?
suspectus, you have hit the point... echo $SCRIPT gives '/bin/bash'... I've manually written the full path and now it works

Get Yesterday's date in solaris

I am running SunOS.
bash-3.00$ uname -a
SunOS lvsaishdc3in0001 5.10 Generic_142901-02 i86pc i386 i86pc
I need to find Yesterday's date in linux with the proper formatting passed from command prompt. When I tried like this on my shell prompt-
bash-3.00$ date --date='yesterday' '+%Y%m%d'
date: illegal option -- date=yesterday
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
I always get date illegal option, why is it so?
Is there anything wrong I am doing?
Update:-
bash-3.00$ date --version
date: illegal option -- version
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
Try this below thing. It should work
YESTERDAY=`TZ=GMT+24 date +%Y%m%d`; echo $YESTERDAY
Try this one out:
DATE_STAMP=`TZ=GMT+24 date +%Y%m%d`
where GMT is the time zone and you might need to alter the 24 according to the hours difference you have from GMT. Either that or you can change GMT to a time zone more comfortable to you e.g. CST
As larsks suggested, you can use perl:
perl -e 'use POSIX qw(strftime); print strftime "%a %b %e %H:%M:%S %Y",localtime(time()- 3600*24);'
Slightly modified from
http://blog.rootshell.be/2006/05/04/solaris-yesterday-date/
To get YYYYMMDD format use this
perl -e 'use POSIX qw(strftime); print strftime "%Y%m%d",localtime(time()- 3600*24);'
This link explains how to format date and time with strftime
http://perltraining.com.au/tips/2009-02-26.html
A pure bash solution
#!/bin/bash
# get and split date
today=`date +%Y%m%d`
year=${today:0:4}
month=${today:4:2}
day=${today:6:2}
# avoid octal mismatch
if (( ${day:0:1} == 0 )); then day=${day:1:1}; fi
if (( ${month:0:1} == 0 )); then month=${month:1:1}; fi
# calc
day=$((day-1))
if ((day==0)); then
month=$((month-1))
if ((month==0)); then
year=$((year-1))
month=12
fi
last_day_of_month=$((((62648012>>month*2&3)+28)+(month==2 && y%4==0)))
day=$last_day_of_month
fi
# format result
if ((day<10)); then day="0"$day; fi
if ((month<10)); then month="0"$month; fi
yesterday="$year$month$day"
echo $yesterday
TZ=$TZ+24 date +'%Y/%m/%d' in SunOS.
Playing on Solaris10 with non-GMT environment, I'm getting this:
# date
Fri Jul 26 13:09:38 CEST 2013 (OK)
# (TZ=CEST+24 date)
Thu Jul 25 11:09:38 CEST 2013 (ERR)
# (TZ=GMT+24 date)
Thu Jul 25 11:09:38 GMT 2013 (OK)
# (TZ=CEST+$((24-$((`date "+%H"`-`date -u "+%H"`)))) date)
Thu Jul 25 13:09:38 CEST 2013 (OK)
As You colud see, I have and I want to get CEST , but TZ=CEST+24 giving me wrong CEST data; GMT+24 giving me correct data, but unusable.
To get the proper result, I has to use GMT+22 (wrong command, correct result) or CEST+22 (wrong value, but finnaly correct result for correct TZ)
A pure bash solution given by #olivecoder is very reliable compared to any other solution but there is a mistake to be corrected. when the day fall on 1st of the month the script is failing with date saying "last_day_of_month" in day value. #olivecoder has missed $ in
day=last_day_of_month, that it should be
day=$last_day_of_month;
After this correction it works very good.
Using Timezone -24 is having some issue based on time when use it. in some cases it goes to day before yesterday. So I think its not reliable.

What is the *nix command to view a user's default login shell

What is the *nix command to view a user's default login shell?
I can change the default login shell with chsh, but I don't know how to get what is the user's default shell.
Pseudocode
$ get-shell
/usr/bin/zsh
The canonical way to query the /etc/passwd file for this information is with getent. You can parse getent output with standard tools such as cut to extract the user's login shell. For example:
$ getent passwd $LOGNAME | cut -d: -f7
/bin/bash
The command is finger.
[ken#hero ~]$ finger ken
Login: ken Name: Kenneth Berland
Directory: /home/ken Shell: /bin/tcsh
On since Fri Jun 15 16:11 (PDT) on pts/0 from 70.35.47.130
1 hour 59 minutes idle
On since Fri Jun 15 18:17 (PDT) on pts/2 from 70.35.47.130
New mail received Fri Jun 15 18:16 2012 (PDT)
Unread since Fri Jun 15 17:05 2012 (PDT)
No Plan.
The login shell is defined in /etc/passwd. So you can do:
grep username /etc/passwd
I think what you are looking for is this:
#!/bin/bash
grep "^$1" /etc/passwd | cut -d ':' -f 7
Save that as get-shell somewhere in your path (probably ~/bin) and then call it like:
get-shell userfoo
SHELL variable is used to represent user's current shell
echo $SHELL

Resources