using history command in the shell script - linux

I'm trying to get the command history inside a shell script. It doesn't work unless I take out the #!/bin/bash
Any clues on how I can get it to work, or to achieve the same effect without removing #!/bin/bash?
Anyone know why it works to remove #!/bin/bash?

When you take out the shebang line it's being run by your current shell. Bash won't have any history to report unless you're using an "interactive" shell. Try changing your shebang line to:
#!/bin/bash -i
which will cause bash to start an interactive shell.

Related

linux "enable -n xxx" command works in terminal but not when put into a script

I've found a very strange issue, when in linux terminal I type "enable -n trap", it would disable the trap linux builtin command. But if I put it into a script like
#!/bin/bash
enable -n trap
and then run the script, there's no error but the command is also not disabled. Really appreciate if someone could share what is happening and how to run it in some file instead of directly in the terminal. Thank you!
The enable command only affects the current shell. When you run a script, that script is executed in a new process, so:
A new shell starts
The enable command runs and disables the trap command in that shell
The shell exits
If you want to affect the current shell, your only option is to source the script using the . (or source) command. If the script is named disable-trap.sh and is in your $PATH, you can run:
. disable-trap.sh
You can also provide a full path to the script:
. /path/to/disable-trap.sh
Sourcing a script like this is largely equivalent to typing the same commands in at the command line: it executes the instructions in the script in the current shell, rather than spawning a new process.

How to solve bash error "syntax error at line 3: 'CYBER_UNAME=$' unexpected"?

This error happens when I run a software containing bash script with beggining like this:
#! /bin/sh
CYBER_UNAME=$(uname)
CYBER_UNAME_M=$(uname -m)
I tried to execute these two commands in terminal and it works fine. This error only happens when I run the shell script. What should I do?
The result of 'uname' is SunOS. This shell script cannot be modified since it's protected on our server.
The line
#! /bin/sh
should read:
#!/bin/bash
So, that script will probably never really work.
If you cannot modify the script in situ, you might want to copy it to your local directory and correct it.
Otherwise,
tail +2 scriptname|/bin/bash
might work.

bash is not executed 'at -f foo.sh' command, even with #!/bin/bash shebang

I'm using the 'at' command in order to create 3 directories, just a dumb bash script:
#!/bin/bash
for i in {1..3}
do
mkdir dir$i
done
Everything is ok if I execute that script directly on terminal, but when I use 'at' command as follows:
at -f g.sh 18:06
It only creates one directory named dir{1..3}, taking interval not as an interval but as a list with one element {1..3}. According to this I think my mistake is using bash script due to at executes commands using /bin/sh but I'm not sure.
Please tell me if I'm right and I would appreciate some alternative to my code since even it is useless I'm curious to know what's wrong with at and bash.
The #! line only affects what happens when you run a script as a program (e.g. using it as a command in the shell). When you use at, it's not being run as a program, it's simply used as the standard input to /bin/sh, so the shebang has no effect.
You could do:
echo './g.sh' | at 18:06

shell startup command

I want the following script command to be executed automatically when ever I log in to shell.
script /home/user/mylog_$(date '+%Y%m%d').log
I forget to run this command most of the times :)
Every time you start an interactive login bash shell, it will execute all commands put in ~/.bash_profile
It depends on the particular shell that you are using.
For bash, add the line to ~/.bashrc.

Linux shell strange situation

Does anyone know why the following script works?
#a-random-junk-string
echo HI
The shell executes the echo command, and outputs HI. I thought that since there is no "!" after the "#", the shell would give an error.
If there is no #! specifying a specific interpreter, the kernel will not intercept and launch it with the specified program.
However, the current shell may still interpret it as a command file, which is what you are seeing take place.
When the shell is asked to run a file with the executable bit turned on then it will examine the file and determine if it begins with a shebang #! if it does then it will execute that command which will get it's program text from the remainder of the file.
If the file does not start with a shebang then the shell will attempt to execute it itself. This is what is happening for you and the shell interprets the first line as a comment.

Resources