Running a command in shell script - linux

I have a shell script file (run.sh) that contains the following:
#!/bin/bash
%JAVA_HOME%/bin/java -jar umar.jar
when i try to run it (./run.sh), it gives me following:
umar/bin/run.sh: line 1: fg: no job control
However if I run same command directly on shell, it works perfectly.
What's wrong with the script file?
Thanks

%foo% is not how you do command substitution in a bourne/BASH shell script. I assume you're running this from a Windows command line, which is why it works when you run it directly. Try using proper bourne syntax:
${JAVA_HOME}/bin/java -jar umar.jar

Try turning on monitor mode
set -m

%JAVA_HOME% will substitute a Windows environment variable and is appropriate in a .bat file.
Try the following shell script which should work on most UNIX like systems.
#!/bin/bash
$JAVA_HOME/bin/java -jar umar.jar

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.

Set bash script in Eclipse

I have a bash script .sh which needs to be executed as a Target Simulator in Eclipse. The problem is, if I run the script with sh run.sh command in terminal, it throws Bad Substitution error. But it works perfectly with bash run.sh. Apparently, Eclipse run it with sh command cause it gives the same error in console. But how can I make Eclipse to run the script with bash instead?
I'm on Ubuntu 13.10.
bash and sh aren't the same shell. There are many constructs valid in bash that are not understood by sh.
Have you provided a correct sheebang as the first line of your script?
#!/bin/bash
If so -- and if Eclipse insist on running script with sh, you still have the option of wrap your script in a heredoc and pass it to bash explicitly:
sh$ cat run.sh
bash << EOF
#
# Here is your bash script
#
EOF
This is mostly a hack until you find how to instruct Eclipse of using the right shell. I'm sure there is a way!

Need help converting batch file to linux shell script

I am switching my server's hosting provider from Windows Server to Linux Debian and I need help to convert my Run.bat to an executable shell script.
The Windows batch file:
java -cp bin; deps/mail.jar; deps/xstream.jar; deps/xpp3-1.1.4c.jar;
deps/scheduling.jar -server server.Server
When I save this as a shell script it does not properly run when I "Run in terminal", the shell just opens and closes immediately.
In linux, the separator is : instead of ;, so try this instead:
$ java -cp bin:deps/mail.jar:deps/xstream.jar:deps/xpp3-1.1.4c.jar:deps/scheduling.jar -server server.Server
Copy the command without the $. The $ is used to indicate the command belongs in a linux shell.

using history command in the shell script

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.

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.

Resources