How do I search for bash script files without having a specific extension within a folder? - linux

I want to find bash script files under folders in Array.
But bash script files do not have a specified extension.
I wrote something like this:
for i in "${array[#]}"
do
# Here I will write the condition that the file is found in the folder $k
done

If your scripts have #!/bin/bash or #!/bin/sh in their first line (as they should), then you can use the file command to check if a file is a script or not.
For example, take this script:
#!/bin/bash
echo "I am a script!"
Output of file filename.sh will be filename.sh: Bourne-Again shell script, ASCII text executable, which is indicating it is a shell script. Note that the file command does not use the extension of the file to indicate its format, but uses the content of it.
If you don't have those lines at the beginning of your file, You can try to run every file (command: bash filename.ext) and the check if it was run successfully or not by checking the value of the variable ${?}. This is not a clean method but it sure can help if you have no other choices.

The file command determines a file type.
e.g
#!/bin/bash
arr=(~/*)
for i in "${arr[#]}"
do
type=`file -b $i | awk '{print $2}'`
if [[ $type = shell ]];then
echo $i is a shell script
fi
done

Related

How do I execute .AppImage files using regular expressions in bash scripts?

I want to execute a .AppImage program using regular expressions because the .AppImage updates it's name every time I update it.
My code:
#!/bin/bash
./home/myname/Applications/myApplication-*.AppImage
Output:
./launch_myApplication.sh: line 3: ./home/myname/Applications/myApplication-*.AppImage: No such file or directory
The real .AppImage name is "myApplication-2-11-2.AppImage", but as written above: the verison number changes every update.
How do I execute .AppImage files using regular expressions in bash scripts?
run in relative path
cd YOUR-TARGET-DIRECTORY
# save the name in cmd
cmd=$(echo myApplication-*.AppImage)
# run it
./$cmd
run in absolute path
cmd=$(echo /home/myname/Applications/myApplication-*.AppImage)
# WRONG
#./$cmd
# CORRECT
$cmd
safer
#!/bin/bash
filename='/home/myname/Applications/myApplication-*.AppImage';
if [[ -f "$filename" ]]; then
$filename;
else
echo "$filename not found";
fi
NOTE
if your $HOME is /home/myname you need to update your script
#!/bin/bash
# wrong
# ./home/myname/Applications/myApplication-*.AppImage
# right
/home/myname/Applications/myApplication-*.AppImage

How to execute commands read from the txt file using shell? [duplicate]

This question already has answers here:
Run bash commands from txt file
(4 answers)
Closed 4 years ago.
I tried to execute commands read it from txt file. But only 1st command is executing, after that script is terminated. My script file name is shellEx.sh is follows:
echo "pwd" > temp.txt
echo "ls" >> temp.txt
exec < temp.txt
while read line
do
exec $line
done
echo "printed"
if I keep echo in the place of exec, just it prints both pwd and ls. But i want to execute pwd and ls one by one.
o/p am getting is:
$ bash shellEx.sh
/c/Users/Aditya Gudipati/Desktop
But after pwd, ls also need to execute for me.
Anyone can please give better solution for this?
exec in bash is meant in the Unix sense where it means "stop running this program and start running another instead". This is why your script exits.
If you want to execute line as a shell command, you can use:
line="find . | wc -l"
eval "$line"
($line by itself will not allow using pipes, quotes, expansions or other shell syntax)
To execute the entire file including multiline commands, use one of:
source ./myfile # keep variables, allow exiting script
bash myfile # discard variables, limit exit to myfile
A file with one valid command per line is itself a shell script. Just use the . command to execute it in the current shell.
$ echo "pwd" > temp.txt
$ echo "ls" >> temp.txt
$ . temp.txt

Linux shell script to copy last lines of a file in a directory to append to a file in another directory

I have written the below shell script to copy the last lines of a file in a directory to append to a file in another directory
cd /opt/grinder/svn/IAVS/GrinderLogs/GrinderBaseLogs/Lg2Logs
GrinderLG1='rm101sys1lweb22'
GrinderLg2='rm101sys1lweb23'
fileCount=$(ls -l|wc -l)
echo $fileCount
for (( c=0; c<=$fileCount-2; c++ ))
do
Lines=$(more $GrinderLg2"-"$c"-data.log"|wc -l)
Lines1=`expr $Lines - 1`
`"tail -"$Lines1"f /opt/grinder/svn/IAVS/GrinderLogs/GrinderBaseLogs/Lg2Logs/"$GrinderLg2"-"$c"-data.log>>/opt/grinder/svn/IAVS/GrinderLogs/GrinderBaseLogs/"$GrinderLG1"-"$c"-data.log"`
#exec $command
done
When I am executing this script it says no such file or directory at tail command. Actually both the files exist. Please help.
`"tail -"$Lines1"f /opt/grinder/svn/IAVS/GrinderLogs/GrinderBaseLogs/Lg2Logs/"$GrinderLg2"-"$c"-data.log>>/opt/grinder/svn/IAVS/GrinderLogs/GrinderBaseLogs/"$GrinderLG1"-"$c"-data.log"`
change to
tail -"$Lines1"f /opt/grinder/svn/IAVS/GrinderLogs/GrinderBaseLogs/Lg2Logs/"$GrinderLg2"-"$c"-data.log>>/opt/grinder/svn/IAVS/GrinderLogs/GrinderBaseLogs/"$GrinderLG1"-"$c"-data.log
should work.
In my setup i have tested as below way
#!/bin/sh
`"tail -10f filename"`
give error filename not found
But
#!/bin/sh
tail -10f filename
This works fine.

Shell Script and Command Line Discrepancies

I was wondering if there were specific permissions that were associated with a shell script or if some variable references are taken as being syntactically different.
I tried my short renaming script below:
#!/bin/bash
echo "Starting Renaming Script"
for file in ./*
do
rename=$(echo $file | sed 's/\(img_\)\([0-9]*-[0-9]*\)-\([0-9]*\)_\([0-9]*\).jpg/newyears_20\3-\2_0\4.jpg/')
mv $file $rename
done
All it does is rename a few files, but I noticed that it would work on the command line, but not in the shell script when I ran sh rename.sh
I got the error
rename.sh: syntax error at line 7: `rename=$' unexpected
Is variable assignment handled differently in the shell than on the command line?
Different shells handle commands differently. Your script is a bash script (as identified on the first line #!/bin/bash), therefore it needs to be run by bash, not sh.
bash rename.sh

When shell scripts echos "*", why it lists the files and directories?

I have written the following shell script to print "*" on the screen but when I execute the script, it lists all the files and directories in the current directory in which script is located. Can someone tell me why the script lists all the files and directories in the current directory?
#!/bin/bash
TEST="*";
echo $TEST
Because there are missing some "" around the $TEST.
Try echo "$TEST".
It prints all the files and folders because the shell, bash in your case, expands the * before passing it to the command.
The solution is simple:
#!/bin/bash
TEST="*";
echo "$TEST"

Resources