How to run history command in script - terminal

I have one script like
history
test.sh file in that I just put history
Now I run sh test.sh
Then it gives error like : history: command not found
Thanks In Advance

history is a command of the shell, so it should not look for it as if it were an external command (like ls)
I made a quick test and did not experience your problem, although I needed to use
set -o history
in order for history to display.
Can you provide further details?

Related

Problem in executing user defining shell commmand

I am trying to learn the basics of shell. I used vim editro for creating my own list of commands to be executed. Here is the way I created the code
vi mycommands
then inside this file I wrote
cd Documents
I am using macOS Catalina which has zsh by default but switched to bash
So when I write the following command in the terminal:
$ sh +x mycommands
It shows
+cd Documents
The Documents has some files and directories but it is not changing directory.Where am I going wrong?
Any help will be greatly appreciated.
Scripts run like sh myscript execute in a separate sub-shell, not the current shell. Changing directory inside a script will not cause your shell to change directory. If you want to change directory in your shell, you need to run the commands in your shell.
To do that, run:
. ./myscript (sh, bash) or source ./myscript (bash).
See this question.

Add command to bash history every time I log in

I'd like to add a command to my bash history, from my bashrc. I'd like to make sure this command is always there.
I tried to do this, but it appears that changing the history from the .bashrc or .bash_profile scripts is not possible.
Does anyone know how to do this?
Just add
history -s mycommand and its arguments
to your configuration file.

Can't execute history command through Ruby

I am able to get expected output using:
puts `ls` # listing of current directory
But I am unable to get the command history using the similar call:
puts `history` # Error - No such file or directory - history (Errno::ENOENT)
history command is disabled in non-interactive sessions, but you should be able to access history directly form the file:
`cat ${HOME}/.bash_history`
(tested on Ubuntu)
history is not a built-in command in the shell. In zsh, for instance, it’s an alias to this:
fc -l 1
You can use that command from Ruby to get the history. But I don’t think it’ll work how you expect, since the shell that Ruby starts up will have no history!
ls is an "external" command that is usually located in /bin/ls. history on the other hand is not an external command but is built-in into your shell, usually bash or zsh.
Now in the shellouts, Ruby uses the /bin/sh shell by default which does not have history commands available.
Note that even though /bin/sh and /bin/bash are usually the same binary, Bash behaves differently when executed as /bin/bash. That's what you are seeing here.

Unknown bash scripts running when terminal opens?

I am not exactly sure what's happening here - I open a terminal window on my mac and see the following:
Last login: Tue Jun 26 00:36:08 on ttys002
-bash: : command not found
-bash: : command not found
This seems to me like some file is being executed whenever I open a new terminal window, but I have no idea how I'd find this file. Is there some list of files that run when terminal opens that I could find easily? I'd love to know what is happening here (and how it came about in the first place)
grep Sorry $(grep -l Thank /etc/profile /etc/bash* ~/.bashrc ~/.bash_profile ~/.profile) /dev/null
And (when you are lucky) you will find the places where are these strange commands with Thank and Sorry.
It is possible although does these lines are produced during some command substitution.
In that case you will not find the strings. I would recommend then add set -x to ~/.bash_profile to find the string that produces these messages.
Check .bashrc, .profile and .bash_profile. Specifically, I have a feeling you have a String marked with inverted commas, which is then being tried to execute
From the bash manual:
When bash is invoked as an interactive login shell, or as a
non-inter‐
active shell with the --login option, it first reads and executes com‐
mands from the file /etc/profile, if that file exists. After reading
that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile,
in that order, and reads and executes commands from the first one that
exists and is readable.

What is wrong with this .command file?

This is my script to automate the git push of my static blog.
When I run each command one by one in the Terminal, it works. Do I need to add delays, maybe it's going too fast. The pelican command (static website generator) takes quite a lot of time (2 seconds). Is the rest of the script crashing during that?
#!/bin/sh
dropbox
cd blog
pelican . -s /Users/Paul-Arthur/Desktop/Desktop/Dropbox/blog/pelican.conf.py -t subtle
cd output
git add .
git commit -m 'commit'
git push
Updated: Sorry, yeah dropbox is a custom command in my bash_profile (this is not the problem, it works I know ;) ).
Sadly, when I click my script, it executes (but does not work) extremely quickly so I cannot see the errors.
Here is the output from the calepin command. The errors are normal and I expect it to run with that. Do you think that this is the problem? If so what can I do?
familys-imac:blog Paul-Arthur$ pelican . -s /Users/Paul-Arthur/Desktop/Desktop/Dropbox/blog/pelican.conf.py -t subtle
ERROR: Skipping ./articles/aboutme.md: impossible to find informations about 'title'
ERROR: Skipping ./articles/static_sites.md: impossible to find informations about 'title'
familys-imac:blog Paul
It might be due to the « cd » command, since it's not a command, it is a builtin from the shell, and doesn't act like a command.
To debug it, try adding «pwd» command inside your script before and after the «cd» line, to be sure the working directory has change.
It could also be due to the shell you are using, in the shebang (first line of your script), you are using the /bin/sh script. Is it the good one ? When you do it in your shell, you are maybe using another like bash, dash, zsh etc.
To determine that, type that in your current shell :
which `echo $0`
You will get an answer like :
/bin/bash
or something like this. Use this in your shell script :
#!/bin/bash
And try again your script.
Good luck with your project.

Resources