在使用 jq
处理 JSON 数据时,我们常常需要从命令行传递变量。jq
提供了两个选项:--arg
和 --argjson
,它们用于不同类型的数据。本文介绍这两个选项的区别以及如何正确使用它们。
--arg
与 --argjson
1. 使用 --arg
2. 使用 --argjson
如何选择
- 如果值是字符串,使用
--arg
。
- 如果值是数字或其他 JSON 类型,使用
--argjson
。
再来个复杂点的例子
jq -n --arg a "Hello" --argjson b 42 '($a + ($b | tostring))'
在这个例子中,字符串 "Hello"
和数字 42
被拼接成 "Hello42"
(带引号的字符串),因为我们将数字转换为字符串后再进行连接。
When using sed
on Windows, you might run into unexpected behavior, especially with line endings. If you're editing files with Windows-style line endings (\r\n
), sed
can unintentionally modify them, causing issues with tools like git diff
. Here’s how you can use sed
effectively on Windows without breaking your line endings.
The Problem: Unexpected \r\n
to \n
Conversion
On Windows, text files usually use \r\n
(carriage return + line feed) as line endings. However, sed
, depending on its mode, may treat these as Unix-style \n
line endings, removing the \r
and leading to issues like:
- Files appearing changed when they shouldn’t.
- Tools like
git diff
showing extra changes due to the altered line endings.
Solution: Use sed -b
to Prevent Line Ending Conversion
To avoid this, you need to force sed
to operate in binary mode, which prevents automatic conversion of line endings.
Example:
sed -b 's/old_text/new_text/' filename.txt
In this example:
- The
-b
flag ensures sed
works in binary mode, preserving the \r\n
line endings.
- The
s/old_text/new_text/
part performs the actual substitution.
set -x && if cd flutter; then git pull && cd .. ; else git clone https://github.com/flutter/flutter.git; (cd flutter && git fetch --tags && git checkout 3.22.3); fi && ls && flutter/bin/flutter doctor && flutter/bin/flutter clean && flutter/bin/flutter config --enable-web && cp .env.production .env && sed -i "s/VERSION_PLACEHOLDER/`git rev-parse --short HEAD`/" .env && flutter/bin/flutter build web --web-renderer html --base-href="/" --release
As a programmer, I often get so focused on solving problems that I forget to prepare for standups. The daily meeting doesn't allow extra time for preparation. If you struggle to remember what you did yesterday, like I did, this short worklog script can help.
The Worklog Script
Save the following script as worklog
and put it into your ~/bin
or any other bin directory in your PATH environment variable. Don't forget to make it executable with chmod +x worklog
.
#!/usr/bin/bash
days=1
if [ "$1" != "" ]; then
days=$1
fi
echo
echo -n "On project $(git remote -v | head -n1 | awk '{print $2}' | sed -e 's,.*:\(.*/\)\?,,' -e 's/\.git$//')"
echo
echo
git --no-pager log --no-merges --pretty=tformat:"- %ad | %s%C(auto)%d [%h]" --date=short --reverse --all --since=${days}.days.ago --author="$(git config user.name)" 2>&1
echo
How It Works
-
Default Days Setting:
days=1
if [ "$1" != "" ]; then
days=$1
fi
This part sets the default number of days to 1. If you provide a different number as an argument when running the script, it updates the days variable accordingly.
-
Fetching Project Name:
echo
echo -n "On project $(git remote -v | head -n1 | awk '{print $2}' | sed -e 's,.*:\(.*/\)\?,,' -e 's/\.git$//')"
echo
This part retrieves the project name from the Git remote URL and displays it. It uses a combination of git remote -v
, awk
, and sed
commands to extract and format the project name.
-
Generating the Git Log:
git --no-pager log --no-merges --pretty=tformat:"- %ad | %s%C(auto)%d [%h]" --date=short --reverse --all --since=${days}.days.ago --author="$(git config user.name)" 2>&1
This part generates the Git log for the specified number of days. It includes:
--no-pager
: To prevent paging the output.
--no-merges
: To exclude merge commits.
--pretty=tformat
: To format each commit log line.
--date=short
: To display dates in short format.
--reverse
: To show the oldest commits first.
--all
: To include all branches.
--since=${days}.days.ago
: To limit the log to commits from the specified number of days ago.
--author="$(git config user.name)"
: To filter commits by the current Git user.
-
Outputting the Result:
echo
Finally, it prints a blank line for better readability of the output.
Running the Script
To see what you have done since 1 day ago, simply run:
worklog
If you want to see the work done in the last 7 days, run:
worklog 7
Handling Multiple Projects
If your work involves multiple projects, the following script can help:
Save the following script as wlog
and put it into your ~/bin
directory. Don't forget to make it executable with chmod +x wlog
.
#!/bin/bash
# Define an array with the project directories
project_dirs=(
/d/git/xemt-core/dfx
/d/git/apollo/mock
)
# Loop through each project directory and run the commands
for dir in "${project_dirs[@]}"; do
(cd $dir && worklog "$@" && cd - >/dev/null)
done
How It Works
-
Project Directories:
project_dirs=(
/d/git/xemt-core/dfx
/d/git/apollo/mock
)
Define an array containing the directories of your projects.
-
Loop Through Projects:
for dir in "${project_dirs[@]}"; do
(cd $dir && worklog "$@" && cd - >/dev/null)
done
Loop through each directory, change to that directory, run the worklog
script with any passed arguments, and then change back to the previous directory. The cd - >/dev/null
part suppresses output from the directory change.
This script helps me quickly summarize my work for daily standup meetings. Customize the project directories in the wlog
script to match your projects and streamline your standup preparation.
Click the title to see the details. By the way, this solution not only works on windows, but also works on Linux platforms: You need to find the correct place on linux platform to add the parameter though.