Using at to run a .txt file or command with flags - linux

I need to run (in bash) a .txt file containing a bunch of commands written to it by another program, at a specific time using at. Normally I would run this with bash myfile.txt but of I choose to run at bash myfile.txt midnight it doesn't like it, saying
syntax error. Last token seen: b
Garbled time
How can I sort this out?

Try this instead:
echo 'bash myfile.txt' | at midnight
at reads commands from standard input or a specified file (parameter -f filename); not from the command line.

Related

How can I write a command to shell script from a shell script without evaluating the command

I'm struggling with passing a shell command. Specifically, I have written a shell file that the user will run. In it, another shell file is written based on the user inputs. This file will then be passed to a command that submits the job.
Now in this internal shell file I have a variable containing a function. However, when I run the user shell script I can't get this function to pass in a way that the internal shell file can use it.
I can't share my work but I'll try to make an example
#User shell script
cat >test.txt <<EOF
#a bunch of lines that are not relevant
var=`grep examples input.txt`
/bin/localfoo -${var}
EOF
# pass test.txt to localfoo2
/bin/localfoo2 /test.txt
When I run the 'User Shell Script' it prints that grep can't find the file, but I don't want grep to be evaluated. I need it to be written, as is, so that when the line '/bin/localfoo2 /test.txt' is read, grep is evaluated.
I've tried a number of things. I've tried double back ticks, i've tried using 'echo $(eval $var)'. But none of the methods I've found through googling have managed to pass this var in a way that will accomplish what I want.
Your help is much appreciated.
You can try with single quote (').
You have to put the single quote in before the grep command and end of the grep command like below.
#User shell script
cat >test.txt <<EOF
#a bunch of lines that are not relevant
var='`grep examples input.txt`'
/bin/localfoo -${var}
EOF
# pass test.txt to localfoo2
/bin/localfoo2 /test.txt
I did not understand where you have to execute that grep command.
If you want to execute the grep command inside the localfoo script, I hope this method will help.

Unix: Last command - How do I execute a sub command for file input?

I am trying to read an on old last file, but when I use the command below
it does not work as expected. Is there a way I can do it in one line?
last -f `zcat /var/log/wtmp.1.gz`
I want to be able to read the file without decompressing the file.
The long version is the following:
zcat /var/log/wtmp.1.gz > /var/tmp/login
last -f /var/tmp/login
You cannot feed last via stdin in bash.
# this won't work
# zcat /var/log/wtmp.1.gz | last-
Side note: However with zsh shell you could
last -f =(zcat /var/log/wtmp-20130827.gz)
Based on https://unix.stackexchange.com/questions/88343/how-to-tell-the-last-command-to-read-from-stdin

What is the meaning of pwd|sed -e?

I found the below snippet at the .sh file of my project to define some path :
PGMPATH=`pwd|sed -e "s#/survey1##" `
What does the above line means ?
Reference of PGMPATH is used as below :
LIBS="${LIBS}:${PGMPATH}/edmz-par-api_1.4.jar"
LIBS="${LIBS}:${PGMPATH}/commons-logging.jar"
If it is telling the path where the jar file is located , please explain how it works .
So first you should know that this is two commands - pwd and sed -e "s#/survey1##" - and these two commands are being run together in a pipeline. That is, the output of the first command is being sent to the second command as input.
That is, in general, what | means in unix shell scripts.
So then, what do each of these commands do? pwd stands for "print working directory" and prints the current directory (where you ran the script from, unless the script itself had any cd commands in it).
sed is a command that's really a whole separate programming language that people do many simple text-processing commands with. The simple sed program you have here - s#/survey1## - strips the string /survey1 out of its input, and prints the result.
So the end result is that the variable PGMPATH becomes the current directory with /survey1 stripped out of it.

store all the data in terminal to text file by tee command or equivalent tool

I learnt that a tee command will store the STDOUT to a file as well as outputs to terminal.
But, here the problem is every time I have to give tee command, for every command I give.
Is there any way or tool in linux, so that what ever I run in terminal, it should store the command as well as output. (I used tee command in MySQL, where it will store all the commands and outputs to a file of that entire session. I am expecting a tool similar to this.)
Edit:
When I run script -a log.txt, I see ^M characters as well as ^[ and ^] characters in log.txt file. I used various dos2unix, :set ff=unix, :set ff=dos commands, but they didn't helped me in removing these ^[, ^] characters.
Is there any method, I can directly get the plain text file (with out these extra chars).
OS: RHEL 5
You can use script command which writes everything on file
script -f log.txt
you could use aliases like such alias ls="ls;echo ls >>log" so every time you run ls it runs echo ls >>log too.
But script would probably be better in this case, just dont go into vi while you are in script.

What can I use to capture every command I run in bash (a-la history)

I know history will capture commands that I run, but it is shell specific. I work with multiple shells and multiple hosts and would like to write a small script which, after every command I run, dumps that command to some file along with the host name. This way, i can implement my own history command which reads from that file, and can take a host as an argument which would be handy for me. I'm not sure how to get the first part though..i.e., get every shell command I type to trigger a "dump that command into a file" part. Any ideas?
Thanks
In bash, the PROMPT_COMMAND environment variable contains a command that will be executed before the PS1 prompt is displayed. So yours could be something like history | tail -n1 | perl -npe 's/^\s+\d+\s+//' | yourcommand HOST
The script utility should solve your problem. It records everything you type and all that is printed on the terminal in a file (even including terminal control codes, so if you cat that file on the console, you even reproduce the original text colors).

Resources