simply bash script doesn't work - bash

#!/bin/bash
echo "hello"
I run this script and I have got only empty space why? I am sure my code is okay, so why don't show text's?

please specify what the problem is.
If your script doesn't run:
For running scripts you should make it executable:
chmod +x script.sh
Another thing that you should know is about SELINUX you should make sure it is sat or not...and specify at which directory it is running?
please run this command and let me know:
ls -l scipt.sh
IF echo command doesn't work:
At first run it in your terminal and see the result...may be the command echo is removed or not...
try to run this bash:
#!/bin/bash
echo "hello"
echo $?
and try this one:
#!/bin/bash
STR="Hello World!"
echo $STR
And let me know about output.
better to ask your question at unix Q&A or superuser

Execute the below commands in the terminal to troubleshoot.
Check to see what shell you are on.
The below command would tell you your current shell.
echo $SHELL
Once you confirm that make sure that you have the bash shell present
in the /bin directory.
/bin/bash
echo "hello"

make sure u've already made your content to your script file. In some case, if u use some editor like vscode it has some option that won't save content to the file.If this is your case, u may change the option.

I ran into the same puzzling issue when running my hello world script from a terminal window, using Ubuntu. I was editing the file using gedit, giving all permissions from the properties gedit functionality, and when running ./hello_world.sh, further chmoded to +x from a terminal window as well, it simply wouldn't show the result of my echo "Hello World" command.
Needless to say as I have been through all the above answers and all showed my script should have worked.
I simply solved it by editing the script using vim from a terminal window! Then chmod +x again just in case, ran the script and Hello World finally appeared!
But I don't know why it didn't work using the gedit edition;

Related

Failure in executing shell script producing output on Ubuntu on Windows

I have had a hard time in executing my shell scripts on bash on Ubuntu on windows 10. The script is very simple:
# file name: submission.sh
echo "Hello world" > output.txt
When I executed it with a command sh submission.sh, it gave me an error:
$ sh submission.sh
: Directory nonexistentssion.sh: cannot create output.txt
However, when I changed the script into
# file name: submission.sh
echo "Hello world"
and executed it with the same command sh submission.sh, it gave me the right output
$ sh submission.sh
Hello world
It seems like bash on Ubuntu on Windows cannot get it right when the script involves directing the output to a file. Is there any solution or workaround to this?
EDIT:
Details on my system:
Program: "Bash on Ubuntu on Windows"
OS: Windows 10 Version 1709
EDIT:
Typing the command directly on the terminal works, i.e.
$ echo "Hello world" > output.txt
$ cat output.txt
Hello world
I still wants to put the commands on a file and execute the file instead of writing the command directly to the terminal, and this is still unsolved.
You appear to have mangled text in nonexistentssion.sh: and No such file or directorytput.txt which suggests you might have Windows line-endings in the file \r. If you created the script using a Windows program (like Notepad) then that could be the case.
If you have dos2unix then run it on your script and try again.
By the way for future reference, running sh is not the same as running bash. In this case it would have made no difference, but sh is a POSIX shell, full bash has many extensions which will not work under sh.
Some platforms run sh as a symbolic link to bash which fools people into thinking they are the same, but bash detects this and switches to POSIX mode when running as sh. It is a common issue here.
In your submission.sh file, is better to add a shebang as first line.
Another thing you want to consider if you want to use sh instead of bash is to replace echo with printf, for portability
Your code should look something like:
#!/bin/bash
printf "Hello world!\n" > output.txt
You can call it simply by ./submission.sh and because of the shebang, your terminal will know how to open it [:
P.S. Keep in mind that because of the standard umask in Ubuntu, you might want to execute chmod u+x submission.sh before running it.
Also, notice that your error is probably caused by a permissions issue.
Try adding write permissions in the folder you are launching the script.

Run a Bash Script automatically upon login on Unix

I have a bash script that I've written that works if I execute from the terminal, but I want to be able to have this script ran when I login to my system. I have seen other questions similar to this, but the proposed solutions did not work for me. I tried adding the script path to my ~/.profile and the script still is not being ran. Anyone have an example or documentation on how to do this?
Side note I am using Unix
If you already had a ~/.profile file it should be fine to just add it there. Otherwise look for a ~/.bash_profile and add the line there.
Did you make sure that your file is executable? Otherwise it will not work.
Here is an example code (make sure to adapt to your needs):
echo "echo 'foo'" > /tmp/execute_me
chmod u+x /tmp/execute_me
echo "/tmp/execute_me" >> ~/.profile
login from another console (for safety), and you should see "foo" printed in your console somewhere.
If you want your script to be executed whenever a shell is used (even not interactive, you should add the code to the ~/.bashrc, read this for details: https://unix.stackexchange.com/questions/129143/what-is-the-purpose-of-bashrc-and-how-does-it-work)
The script should be called .profile and in your scripts directory.
I don't if your system supports it, but calling scripts (as root or as a common user , via su - user -c "command") from /etc/rc.local works great on my "nix" system. You might want to add some delay and possibly pass the display variable as well. Example of rc.local entry:
sleep 20 && bash -c "env DISPLAY=:0.0 nohup /folder/script &"

Can't start script via screen command

I have a script, and I want to make it work in background after boot. Therefore, I added the command below into rc.local. However, it doesn't work.
sudo -iu executerroot screen -dmS test bash -c "bash /home/pi/FileServer/Run; exec bash"
Also I've tried commands in https://askubuntu.com/questions/261899/run-a-screen-session-on-boot-from-rc-local site but no one worked for me. If I execute
sudo -iu executerroot screen -dmS test bash -c "bash /home/pi/FileServer/Run; exec bash"
I get this:
bash: /home/pi/FileServer/Run/: No such file or directory
while it doesn't work for real files. When I type "screen -r" I get just blank bash screen if a file exists.
I can't figure out why it doesn't work. Please explain why it happens and how can I solve this.
Thanks...
After years, I got a similar problem today. This time I was trying to call the screen program inside crontab. I realized I needed absolute paths and correct bash version in crontab as well as in the script itself.
I think I had done a similar mistake 6 years ago. I wrote similar program today, probably, due to different environment variables, script didn't be able to find programs (in my case openvpn). Even program &> output.txt didn't work. The problem was the different bash version not supporting output redirection. I used /usr/sbin/bash -c "command" to cope with it. Also, openvpn is not in the path in that crontab environment, so I had to call it with absolute path (like /usr/sbin/openvpn). Then, it worked.

executable file doesn't print anything

I've written a simple script, that basically looks like this:
#!/bin/bash
echo Hello World
I'm trying to run this in my unix terminal but it basically does nothing. no errors, no printing, nothing
[solgag#t2 ~]$ olga
[solgag#t2 ~]$
any ideas?
Try ./test instead. If you run just test, bash will look for an executable named test in $PATH and it will find it (or maybe execute its own built-in?) as test is a standard command in UNIX.
if you shell script name is olga you need to run in terminal as
$./olga
To run the script as specified above you need to have executable permissions you can add executable permission using chmod command
$chmod u+x ./olga
You can also run a bash script using sh command
$sh olga

Activating a VirtualEnv using a shell script doesn't seem to work

I tried activating a VirtualEnv through a shell script like the one below but it doesn't seem to work,
#!/bin/sh
source ~/.virtualenvs/pinax-env/bin/activate
I get the following error
$ sh virtualenv_activate.sh
virtualenv_activate.sh: 2: source: not found
but if I enter the same command on terminal it seems to work
$ source ~/.virtualenvs/pinax-env/bin/activate
(pinax-env)gautam#Aspirebuntu:$
So I changed the shell script to
#!/bin/bash
source ~/.virtualenvs/pinax-env/bin/activate
as suggested and used
$ bash virtualenv_activate.sh
gautam#Aspirebuntu:$
to run the script .
That doesn't throw an error but neither does that activate the virtual env
So any suggestion on how to solve this problem ?
PS : I am using Ubuntu 11.04
TLDR
Must run the .sh script with source instead of the script solely
source your-script.sh
and not
your-script.sh
Details
sh is not the same as bash (although some systems simply link sh to bash, so running sh actually runs bash). You can think of sh as a watered down version of bash. One thing that bash has that sh does not is the "source" command. This is why you're getting that error... source runs fine in your bash shell. But when you start your script using sh, you run the script in an shell in a subprocess. Since that script is running in sh, "source" is not found.
The solution is to run the script in bash instead. Change the first line to...
#!/bin/bash
Then run with...
./virtualenv_activate.sh
...or...
/bin/bash virtualenv_activate.sh
Edit:
If you want the activation of the virtualenv to change the shell that you call the script from, you need to use the "source" or "dot operator". This ensures that the script is run in the current shell (and therefore changes the current environment)...
source virtualenv_activate.sh
...or...
. virtualenv_activate.sh
As a side note, this is why virtualenv always says you need to use "source" to run it's activate script.
source is an builtin shell command in bash, and is not available in sh. If i remember correctly then virtual env does a lot of path and environment variables manipulation. Even running it as bash virtualenv_blah.sh wont work since this will simply create the environment inside the sub-shell.
Try . virtualenv_activate.sh or source virtualenv_activate.sh this basically gets the script to run in your current environment and all the environment variables modified by virtualenv's activate will be available.
HTH.
Edit: Here is a link that might help - http://ss64.com/bash/period.html
On Mac OS X your proposals seems not working.
I have done it this way. I'am not very happy with solution, but share it anyway here and hope, that maybe somebody will suggest the better one:
In activate.sh I have
echo 'source /Users/andi/.virtualenvs/data_science/bin/activate'
I give execution permissions by: chmod +x activate.sh
And I execute this way:
`./activate.sh`
Notice that there are paranthesis in form of ASCII code 96 = ` ( Grave accent )
For me best way work as below.
Create start-my-py-software.sh and pest below code
#!/bin/bash
source "/home/snippetbucket.com/source/AIML-Server-CloudPlatform/bin/activate"
python --version
python /home/snippetbucket.com/source/AIML-Server-CloudPlatform/main.py
Give file permission to run like below.
chmod +x start-my-py-software.sh
Now run like below
.start-my-py-software.sh
and that's it, start my python based server or any other code.
ubuntu #18.0
In my case, Ubuntu 16.04, the methods above didn't worked well or it needs much works.
I just made a link of 'activate' script file and copy it to home folder(or $PATH accessible folder) and renamed it simple one like 'actai'.
Then in a terminal, just call 'source actai'. It worked!

Resources