sh script escaping - linux

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

Related

./ Dot Slash not working to execute Shell Scripts

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

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;

coloring terminal with shell script

Someone can explain me why when I copy and paste the following command in the terminal it displays the colorful test correctly, but when I run it via sh myscript.sh it does not display the colored text?
blue='\e[1;34m'
NC='\e[0m'
echo -e "${blue}Test${NC}"
EDIT
Sudo is not the problem. If I copy the above and paste directly into the terminal, everything works. If you run through file, sh myscript.sh not work
Probably because sh isn't bash on your system.
$ file /bin/sh
/bin/sh: symbolic link to `dash'
Try
bash myscript.sh
Your interactive shell seems to be GNU Bash, while sh is a generic POSIX shell, which actually may be dash, busybox sh or something else. The problem is that neither -e option for echo nor \e are POSIX-compliant.
But you can easily use printf instead of echo -e (do not forget to explicitly specify newline character \n) and \033 instead of \e:
blue='\033[1;34m'
NC='\033[0m'
printf "${blue}%s${NC}\n" 'Test'
Or, of course, you can just use bash (as Elliott Frisch suggested) if you are sure that it would be available on target system.
Also I should point out, that what you done is not right way to run shell scripts at all. If you’re writing a standalone script, then you’d better to use hashbang and set execution bit to file.
$ cat myscript
#!/bin/sh
blue='\033[1;34m'
NC='\033[0m'
printf "${blue}%s${NC}\n" 'Test'
$ chmod +x myscript
$ ./myscript
But if you’re writing a command sequence (a macros, if you will) for interactive shell, there is source (or simply .) command:
$ source myscript
(Then all of above about POSIX-compliance does not matter of course.)

bad interpreter: Permission denied in shell scripting ls ubuntu

I am quite new to the shell scripting.
So I am writing the shell script to list all files available in the directory using ls command.
but I am getting the error bad interpreter: Permission denied
#!/home/gaurav
echo "Welcome bash shell scripting"
ls
echo "this complets the listing of directories"
I want to get the list of "/home/gaurav" this path
Thanks
This line...
#!/home/gaurav
... means "instead of using /bin/bash, use /home/guarav as the program to run this file". This is not what you want. What you want is either:
cd /home/gaurav # at the top, or
ls /home/gaurav # between echoes
Problem is this line:
#!/home/gaurav
This is called shebang and it should be the bash/shell interpreter like this:
#!/bin/bash
one that interprets and executes your script. Since /home/gaurav is not a valid interpreter you're getting that error.
You probably want this in your script:
ls /home/gaurav
to list all files/directories in /home/gaurav path.
Either add #!/bin/bash or #!/bin/sh instead of #!/home/gaurav line while starting script.
Because, while running shell script, you have to give path of which bash or sh are you going to run to execute that script.

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