I'm fairly new to writing scripts on Mac OS so apologies if the answer is fairly obvious.
I want to add a date variable to my script on Mac OS that will output the current date in YYYYMMDD format.
On Windows I would write this as:
set now=%date:~6,4%%date:~3,2%%date:~0,2%
%now%
How would I do something similar when writing a .command file on Mac OS?
You can run this in a terminal/bash script
myDate=$(date +%F)
echo $myDate
It outputs a date like:
2021-05-21
Alternatively you can specify a formatter:
myDate=$(date +%Y%m%d) # to remove the dashes
Starting in bash 4.1 or 4.2, the builtin printf can emit the formatted time:
printf '%(%Y%m%d)T' -1 # => 20210521
Since MacOS ships with bash 3.2 by default that means you'll need to install a newer version yourself. With Homebrew that is as easy as brew install bash
Related
I am working on bash scripts on both linux and mac.
I run this command on remote server with linux OS and it just work perfect.
CURRENT_TIME=$(date '+%s%N')
echo "$CURRENT_TIME"
However, when I run the same command on mac terminal, it shows this error:
1654778186N: value too great for base (error token is "1654778186N")
It look like mac terminal did not recognized the '%N'. What should I do to fix the issue on mac terminal?
%N is a GNU extension to the POSIX standard, as clarified in GNU's own documentation: https://www.gnu.org/software/coreutils/manual/html_node/Time-conversion-specifiers.html
The POSIX version of date, doesn't include %N: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/date.html
If I recall correctly, POSIX-certified systems (and macOS is certified) cannot add custom extensions to POSIX tools to guarantee portability across systems.
The ${parameter[^|^^|,|,,][pattern]} parameter substitution is giving me a bad substitution error.
$ echo $greeting
hello world
$ echo "${greeting^}."
-bash: ${greeting^}.: bad substitution
I updated to the latest bash version and keep getting the error.
GNU bash, version 4.4.19(1)-release (x86_64-apple-darwin17.3.0)
I've looked everywhere and the only suggestion I've found is making sure it's running bash 4.
$ echo $SHELL
/bin/bash
I'm running macOS High Sierra.
Your default shell is not the bash shell (downloaded from brew install bash) that contains the v4 which supports the parameter expansion syntax you are referring to.
On macOS echo $BASH_VERSION will tell you the version of the current shell. bash --version tells you the version of the first bash in your $PATH. So the way you were looking at the version was not telling you the version that you were running.
You need to add the recent version of bash to the file /etc/shells as the last line and use the command to set the shell as the default on Terminal
chsh -s /usr/local/bin/bash "$USER"
After this close and re-open the Terminal to make it effect. Without adding this default option in your Terminal, you could only use the recent bash only on scripts with interpreter she-bang set to #!/usr/local/bin/bash
See also this Ask Different answer to - Update bash to version 4.0 on OSX
I tried sw_vers but it only prints the information to the terminal. I cannot use it, as I have to store it in a variable and then compare.
Based on version I have to move files to appropriate location in file system.
What's the correct way to achieve this?
This is the way to do it:
v=$(sw_vers -productVersion)
echo $v
10.9.3
I have this bash script that loops through the files in the current directory and extracts the date part from the filename, then uses (Unix) touch command to modify (or update) that file's modification-date (mtime) to this date.
Filename example
Electric company name - bill - 2014-03-22.pdf
Bash script:
I save this bash script as _save_file_mtime_from_filename.sh (add chmod +x to it) and put in the directory where I'd like to change the file's modification time. And then run it from the command-line.
#!/bin/bash
CURRENT_DIR=$(dirname $_)
cd $CURRENT_DIR
for f in *
do
# Strip the file extension
d=${f%.*}
# Strip the last 10 characters
d=${d:${#d}-10}
# Check the format / mask
if [[ $d = ????-??-?? ]] ; then
# Strip the dash
d=${d//-}
# Run `touch` on the file with the extracted date format
# and add `0000` to the file date
touch -t ${d}0000 "$f"
fi
done
Now I'm searching for a Windows version of this script
I've search the net (and Stackoverflow). Found some related questions like https://stackoverflow.com/questions/51435/windows-version-of-the-unix-touch-command and https://superuser.com/questions/251470/windows-recursive-touch-command/251507#251507
Does anyone have any idea for a Windows version using a _save_file_mtime_from_filename.bat executable version that does essentially the same thing?
With a little tweaking and help of a (Mac) Automator action, saved as an 'application', you can even trigger this script in the Mac Finder from the right-mouse button (https://discussions.apple.com/thread/5287944?start=15&tstart=0). Sweet!
Install Cygwin, or install binaries from GnuWin32 or UnixUtils
I agree with #konsolebox. Just install Cygwin, and you can run your script with no changes.
However, if you don't want to go that route, you should be able to install "coreutils" from GnuWin32. Coreutils contains "touch", among other things. Writing a DOS batch file to emulate what you've written above might be a little painful, though. The part that looks tricky to me is [[ $d = ????-??-?? ]]. You'll have to do something creative to match that, perhaps using grep, which you can get from the same page. Now you're installing a couple things, and doing a lot of work, though. Installing Cygwin would be easier.
In a bash shell on linux, if I type:
echo $DISP
and hit tab, it completes to:
echo $DISPLAY
It doesn't work in bash on OSX 10.5, and I'm using the same .bashrc
Is there a shell option or setting I can use to set this.
you may need to install bash_completion from darwin ports (http://bash-completion.darwinports.com/)
I don't know if its what you want, but how about this?