How do I know the script file name in a Bash script?
me=`basename "$0"`
For reading through a symlink1, which is usually not what you want (you usually don't want to confuse the user this way), try:
me="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")"
IMO, that'll produce confusing output. "I ran foo.sh, but it's saying I'm running bar.sh!? Must be a bug!" Besides, one of the purposes of having differently-named symlinks is to provide different functionality based on the name it's called as (think gzip and gunzip on some platforms).
1 That is, to resolve symlinks such that when the user executes foo.sh
which is actually a symlink to bar.sh
, you wish to use the resolved name bar.sh
rather than foo.sh
.
bash script to check file name begins with expected string
In bash
, you can get the first 7 characters of a shell variable with:
${sourceFile:0:7}
and the last four with:
${sourceFile:${#sourceFile}-4}
Armed with that knowledge, simply use those expressions where you would normally use the variable itself, something like the following script:
arg=$1
shopt -s nocasematch
i7f4="${arg:0:7}${arg:${#arg}-4}"
if [[ "${i7f4}" = "adusers.txt" ]] ; then
echo Okay
else
echo Bad
fi
You can see it in action with the following transcript:
pax> check.sh hello
Bad
pax> check.sh addUsers.txt
Bad
pax> check.sh adUsers.txt
Okay
pax> check.sh adUsers_new.txt
Okay
pax> check.sh aDuSeRs_stragngeCase.pdf.gx..txt
Okay
How to find filename in file content with bash script?
on given file ('+')
+
executes the command on all found files. The {}
is replaced by the list of files in current directory.
Debug with -exec echo grep -il {} +
.
You want:
-exec grep -Fil {} {} ';'
To search the filename {}
as a pattern in the file named {}
. The ';'
terminates the command.
I also added -F
to interpret pattern literally.
How to find out name of script called (sourced) by another script in bash?
Change file2.sh
to:
#!/bin/bash
echo "from file2: ${BASH_SOURCE[0]}"
Note that BASH_SOURCE
is an array variable. See the Bash man pages for more information.
How to get the Directory name and file name of a bash script by bash?
#!/bin/bash
echo "/$(basename "$(dirname "$0")")/$(basename "$0")"
echo
echo
read -r
Output:
/Dirname/Filname.Extension
Get only file name in variable in a for loop
You need two lines; chained operators aren't allowed.
f=${f##*/} # Strip the directory
f=${f%%.*} # Strip the extensions
Or, you can use the basename
command to strip the directory and one extension (assuming you know what it is) in one line.
f=$(basename "$f" .txt)
Related Topics
How to Remove Cached Credentials from Git
How to Turn Off Echo While Executing a Shell Script Linux
How to Write a Bash Script to Restart a Process If It Dies
Deploying Yesod to Heroku, Can't Build Statically
Encryption/Decryption Doesn't Work Well Between Two Different Openssl Versions
Docker Can't Connect to Docker Daemon
Performing Http Requests With Curl (Using Proxy)
Makefiles With Source Files in Different Directories
Why Is a Tilde in a Path Not Expanded in a Shell Script
What Does Localhost Means Inside a Docker Container
How to Display Number to Two Decimal Places in Bash Function
How to Send a File as an Email Attachment Using Linux Command Line
How to Run a Shell Script At Startup
How to Install Gcc 4.9.2 on Rhel 7.4