Bash script not running - linux

I am trying to learn bash scripting and I'm using Ubuntu Linux. I have written a simple Bash file to count the number of files in current directory. I have written the following script in a file:
#! /bin/bash
ls -1 | wc -l
And saved the file with the name countFile.
But when I am trying to execute the script using ./countFile it is not executing. It shows the following error:
bash: ./countFile: Permission denied
The countFile is in my home directory so why I haven't the permission. Am I doing something wrong or missing some important thing? Moreover, the ls -1 | wc -l command gives me the correct output when I run it from the terminal.
So how can I run the countFile script?

While you are giving like this,
./countfile
You have to make that file as executable using chmod.
chmod +x countfile
Or else you can use the other interpreter like this.
sh countfile

while executing the file we need a execute permission for that file,
we can change the permission or
we just run as
. countfile
hew . will represent the current working shell

Related

Run bash script in current directory Linux

Why can't I run a bash script in the current directory I'm in?
Whenever I run the script the commands are executed in the home directory.
The only answers I found are included below.
I do use the zsh shell. I don't know if that changes anything.
Thanks in advance!
What I have tried so far:
#!/bin/bash
touch test.txt
#!/bin/bash
cd $PWD
touch test.txt
#!/bin/bash
variable = $PWD
cd $variable
touch test.txt
#!/bin/bash
variable= pwd
cd $variable
touch test.txt
#!/bin/bash
cd -
touch test.txt
If I run the script for example from /home/user/dir1/dir1.1 the test.txt file is created in the home directory (/home/user) and I get redirected to the home directory as well.
in bash there are two things to do:
ensure that the shell script file is saved properly and is chmod'd to be an executable.
To do so, save the file (e.g. script.sh) with the code you want, and then run chmod +x script.sh to make linux understand that this file is an executable.
call the executable properly using the ./script.sh command. alternatively, you can also call the script from remote folder by calling it using the absolute path the script is in (e.g. /folder/folder/folder/script.sh).
This should execute the file. from there, it's about your code and if you need help there, please update your question.

bash: ./shelllab2.sh: No such file or directory

I am running shell scripting program using git bash on windows 10.
I am sure I had created my file here then also getting this error.
bash: ./shelllab2.sh: No such file or directory
Check first where you are when you try to access that script:
pwd
ls -alrth
You will see if ./shelllab2.sh is indeed here.

"cannot execute binary file" when trying to run a shell script on linux

I am very new to linux and shell scriprting.
I am trying to run a shellscript from secure shell (ssh) on linux using following commands:
chmod +x path/to/mynewshell.sh
sh path/to/mynewshell.sh
I get this error:
path/to/mynewshell.sh: path/to/mynewshell.sh: cannot execute binary file.
Tried using this command:
bash path/to/mynewshell.sh
I get the same error.
Tried with this command: su - myusername sh path/to/mynewshell.sh
It is asking for my password and giving me this error: no such file or directory.
1.The result of cat -v path/to/mynewshell.sh is:
^#^#^#^#^#^#^#^#Rscript "$dir"/diver_script.R
done
2.When tried 'less path/to/mynewshell.sh' i got this on my terminal:
#!/bin/bash/Rscript^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#
^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#
for dir in /path/to/* ; do
^#^#^#^#^#^#^#^#Rscript "$dir"/myRscript.R
done
3.When i ran file path/to/mynewshell.sh : i got this "Bourne-Again shell script text executable"
Please give any advice on how I can try executing the shellscript.
chmod -x removes execution permission from a file. Do this:
chmod +x path/to/mynewshell.sh
And run it with
/path/to/mynewshell.sh
As the error report says, you script is not actually a script, it's a binary file.
I was getting the same error running my shell script through a bash interpreter in PowerShell. I ran dos2unix myscript.sh on the shell script, and now it runs ok.
From a proposed duplicate:
run_me.sh.xz: run_me.sh.xz: cannot execute binary file
This is because the file is compressed, as indicated by the .xz extension. You need to remove the compression before the file can be used.
xz -d ./run_me.sh.xz
chmod +x ./run_me.sh # probably not necessary if you already did that before
./run_me.sh
Other compression schemes like gzip (.gz extension), bzip2 (.bz2 extension) etc behave similarly; you just have to know the name of the command to uncompress it, which is of course usually easy to google.
To anyone else having the problem i had.
i was trying to run a 16 bit unicode text file converted to a shell script, this doesn't work as all 16 bit unicode text files have a 0xFFFE marker at the start making mac os not like the file and this gives the “cannot execute binary file” error.
open the text file click on "Format" at the top, go down to "Make Plain Text" click it.
open your terminal type chmod 777 /path/to/file.sh
put in terminal: /path/to/file.sh to run it
That script is simply not a shell script.
A shell script is usually readable and contains shell code.
The output your cat command shows looks indeed like it's a binary of some sort.
As some note, it might be because of a file conversion issue when copying but it looks more like an actual binary to me.
You can check what it is identified as with the file command so:
file path/to/mynewshell.sh
Just start with a clean script and rewrite the code, it looks like you just want to run some R scripts in a directory?
Make sure the R scripts point to the right R script executioner.
In my case I had a bash script that would not execute. The file was originally generated from a find ... -print0 command. Leaving a \0 character the script, removing that character solved my problem.

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