./ Dot Slash not working to execute Shell Scripts - linux

I'm definitely a command line novice. I have recently lost the ability to execute shell scripts using
./script.sh
I am still able to execute shell scripts using:
sh script.sh
My $PATH is as follows:
/Users/goodguy/.pyenv/shims:/Users/goodguy/.rbenv/shims:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/local/bin:/opt/local/sbin:/Users/goodguy/.rvm/bin
I am using MacOS. I'd appreciate any insight into what I may be doing wrong

you can run command chmod 755 script.sh
and then run the script with ./script.sh

Related

Script to execute commands in the terminal

In fact I have a command line to run in a terminal to launch my Twitch point miner which has a graphical feedback in the terminal to know the number of points retrieved and other things.
But I have no idea how to put it in a .sh script
Here are the commands to execute in the terminal.
virtualenv -p python3 venv
source venv/bin/activate
cd /home/pi/Twitch-Channel-Points-Miner-v2
python run.py
If you can help me on this subject, it would be great.
Thanks
To create a script:
Open the terminal.
Go to a folder where you want to have the script cd /path/to/folder.
Create a new file using vi, nano or any other editor. Put the commands in the file. For example, here I use the echo utility to put 2 echo commands into the my_script file echo -e "echo 'command 1'\necho 'command 2'" > my_script.
Add the execute permission to the file chmod +x my_script.
Execute the script using some shell, for example, bash bash my_script.
See the result:
command 1
command 2

Korn Shell path not found

I have a problem with a script ksh (mainScript) that call other scripts.
mainScript:
call ./folder1/folder2/first.sh
error that I get:
./folder1/folder2/first.sh not found [No such file or directory]
I verify with ls -l /folder1/folder2/first.sh and I see that the script exist.
Thank you for helping me.
remove the . from your path and also provide execute permission on the file then you will be able to run the script. like
[root#localhost#] chmod +x /folder1/folder2/first.sh
then run
[root#localhost#] /folder1/folder2/first.sh
Also, you can run your script using following:
ksh /folder1/folder2/first.sh or sh /folder1/folder2/first.sh or bash /folder1/folder2/first.sh etc

How to Start a Bash Script with Fewest Keystrokes

To fire a shell script from the command line, instead typing this at a Linux/Unix command line:
~$ <shell> tale.sh
In my case, using bash:
~$ bash tale.sh
How do you setup profile/defaults/scripts so that the command will run with just:
~$ tale
I know this can be different for different shells. I need the answer for bash.
This is the whole listing for ~/tale.sh:
#! /bin/bash
tail -f ~/lp/_logs/error.log
That file, ~/lp/_logs/error.log, is a PHP error log.
First, you need to rename your script:
mv tale.sh tale
Then, there may be two additional steps:
1) Set the executable bit on your script:
chmod +x tale
2) Make sure your script is in your PATH. For example, you could place it in your bin directory (assuming your bin directory is in your path):
mv tale ~/bin
Once all this is in place your script will run from anywhere, whichever shell you are using.
There is an alternative approach called "alias" which could be use.
alias tale="./tale.sh"
or
alias tale="tail -f ~/lp/_logs/error.log"
This lasts until session is not terminated. It can be persists by making an entry in .bashrc or .bash_aliases file.
You can try with the below one line command :)
chmod +x tale.sh && bash tale.sh;

sh script escaping

From a linux terminal the command:
ls application/js/{a*,b*,c*,d*,e.pocket-secure}
Will list all files in the application/js directory that start with a,b,c,d or e.pocket-secure.
If I place that command in doit.sh and execute:
sh doit.sh
I get:
ls: cannot access application/js/{a*,b*,c*,d*,e.pocket-secure*}: No such file or directory
I think the {} is confusing the shell interpreter, but I tried quoting (single|double) and also tried escaping the {} to no avail.
When you type it in the terminal, I suspect that you are in bash. When you run sh doit.sh, you are changing the characteristics of the shell, either because bash is in compatibility mode or because you are using something like dash. Try doing bash doit.sh.
this syntax work only with bash interpretor you can try this
#/bin/bash
ls application/js/{a*,b*,c*,d*,e.pocket-secure}
and
chmod +x doit.sh
./doit.sh
OR
just type this on shell
bash doit.sh

run linux command succeed but failed in .sh file. "No such file or directory "

I wrote a linux command and it runs perfectly in command line:
/bin/netstat -an | grep '3306' | sed 's/.*/[MYSQLMON]&/' > /home/bbWifiExt/logs/WIFIMonitor.log
however when I copy this code to .sh and run the .sh file i got:
No such file or directory
Can anyone tell me why? Many thanks.
You must either call it as
sh mycommand.sh
or make your shell script executable. Insert #! /bin/sh or #! /bin/bash as the first line and
chmod +x mycommand.sh
before calling
mycommand.sh
for my situation (rename and copied file from windows to linux) the solution was :
dos2unix script.sh
If the first line of the script something like
#!/bin/sh
and the execute bit set i.e.
chmod +x script.sh

Resources