bash: bad interpreter: permission denied - linux

When I try to execute my script I encounter an error:
bash: /home/thai/first3.sh: /home/thai: bad interpreter: permission denied
Here's my script:
#!/home/thai
for file in *
do
if grep -l 'main()' $file
then
more $file
fi
done
exit 0
I believe something's wrong with my script but I couldn't figure it out, can someone help me please. I'm running Ubuntu Linux.

You are declaring an incorrect shebang. The first line of your file should be:
#!/bin/bash
Instead of your HOME directory. In addition, your script could be simplified too. Try this:
#!/bin/bash
for file in $(grep -l 'main()' *); do
more $file
done

Related

Error not being obtained in file

Probably a naive question but I am stuck.
I have a shell script file.sh which contains a simple command(I don't have python installed)
echo $PATH && cd /home/akash/repos/dhvani/ && echo $PWD && python main.py
When I run it as sh file.sh > /tmp/out. I get the the portions of echo in the /tmp/out but not the error. I don't have python installed so I must get error like python: not found in the file as well. But I only get that in the terminal.
Can anyone explain a way how can I log this error as well.
To make stderr as well as stdout go to the file, do this:
sh file.sh > /tmp/out 2>&1

Bash script that will execute a program over all files in a directory

Writing a bash script that will execute the program 'main' over all the files in the directory 'allfiles'. Main is an executable. The results are then directed to the file 'output.dat'.
Whenever I run the bash script I get the error "./main: no such file or directory". The bash script, main, and 'allfiles' directory are all in the '/home/directory/'. I'm not certain as to why i'm getting this error, or if i'm writing the bash script correctly. Any help is greatly appreciated.
#!/bin/bash
for file in /home/directory/allfiles/*
do
./main $file >> output.dat
done
edit: should clarify that 'main' is an executable file produced from a makefile
If you're not in /home/directory when you run the script, then it won't be able to find main. Try
#!/bin/bash
for file in /home/directory/allfiles/*
do
/home/directory/main $file >> output.dat
done
Additionally, if main is not executable, you'll have to get bash to call it manually. Use bash /home/directory/main $file for that, or make main executable using chmod +x main.
I prefer using find in this case of use. You can manage using subdirs ;)
#!/bin/bash
main=./main
basedir=/home/directory/allfiles/
outFile=output.dat
for file in $(find $basedir -type f)
do
$main $file >> $outFile
done

Bash file shows "ln: command not found"

I'm trying to create a bash script to setup my development environment. The script is running as root but I get the error line 11: ln: command not found
#!/bin/bash
#Require script to run as root - doesn't work - syntax error in conditional expression: unexpected token `;'
#if [[ $(/usr/bin/id -u) -ne 0]]; then
# echo "Script must be run as root";
# exit;
#fi
#PHPMyAdmin
PATH="/etc/apache2/sites-available/phpmyadmin.local";
if [ ! -a PATH ]; then
ln -s /home/user/Ubuntu\ One/htdocs/vhosts/phpmyadmin.local PATH;
a2ensite phpmyadmin.local;
fi
PATH=...
Congratulations, you've clobbered how the shell finds commands. Don't do that.
PATH tells the shell where to look for commands. In your case, it looks for ln somewhere in /etc and predictably doesn't find it there.
You should use a different name.

redirecting stdout in shell script

Either it is late in the day for me or I am missing something naive here.
here is a contrived example
#!/bin/bash
command="ls -al > check.txt"
$command
When I run this script on a shell it gives me error I guess due to the ">" operator. Anyway I can redirect the output from inside a shell script. I thought this was very straight forward:
ls -la > temp.txt
ls: cannot access >: No such file or directory
ls: cannot access temp.txt: No such file or directory
#!/bin/bash
command="ls -al"
$command > check.txt
> is a special character in Bash (and most shells). It does not belong to a command.
Here is another way to do it using eval,
#!/bin/bash
command="ls -al > check.txt"
eval $command

How to process file names with variables from a list in a file in Bash

I have a file "FileList.txt" with this text:
/home/myusername/file1.txt
~/file2.txt
${HOME}/file3.txt
All 3 files exist in my home directory. I want to process each file in the list from a bash script. Here is a simplified example:
LIST=`cat FileList.txt`
for file in $LIST
do
echo $file
ls $file
done
When I run the script, I get this output:
/home/myusername/file1.txt
/home/myusername/file1.txt
~/file2.txt
ls: ~/file2.txt: No such file or directory
${HOME}/file3.txt
ls: ${HOME}/file3.txt: No such file or directory
As you can see, file1.txt works fine. But the other 2 files do not work. I think it is because the "${HOME}" variable does not get resolved to "/home/myusername/". I have tried lots of things with no success, does anyone know how to fix this?
Thanks,
-Ben
Use eval:
while read file ; do
eval echo $file
eval ls $file
done < FileList.txt
From the bash manpage regarding the eval command:
The args are read and concatenated together into a single command. This command is
then read and executed by the shell, and its exit status is returned as the value of
eval. If there are no args, or only null arguments, eval returns 0.
you will hit "spaces problem" using the for loop with cat. Manipulate IFS, or use a while read loop instead
while read -r line; do eval ls "$line"; done < file
Change "ls $file" to "eval ls $file" to get the shell to do its expansion.

Resources