1

I want to take a screenshot with import, and save it into a file whose name is current time.

Here is what I've tried:

sunqingyao:~$ date '+screenshot-%y%m%d-%H%M%S.png'
screenshot-170716-173336.png
# OK

sunqingyao:~$ import screenshot-170716-173336.png
sunqingyao:~$ ls -l screenshot-170716-173336.png 
-rw-rw-r-- 1 sunqingyao sunqingyao 250556 Jul 16 17:35 screenshot-170716-173336.png
# OK

sunqingyao:~$ date '+screenshot-%y%m%d-%H%M%S.png' | import
import: missing an image filename `import' @ error/import.c/ImportImageCommand/1293.
# Not OK

For some reason, I want this command to be a one-liner.

Background info:

Actually I'm trying to bind a key to screenshot taking with i3. Here is the relevant part of my ~/.config/i3/config:

# Take a screenshot upon pressing $mod+x (select an area)
# https://i3wm.org/docs/userguide.html#keybindings
bindsym --release $mod+x exec --no-startup-id date '+screenshot-%y%m%d-%H%M%S.png' | import

Seems that I can only bind a key to a command that can fit in only one line, which is why a one-liner is required.

nalzok
  • 369

2 Answers2

1

since import will take the name of the .png as argument constract it and call the import. Think that first wil the $() will be substitute and then will be passed as argument in import

import $(date +'screenshot-%Y%m%d-%H%M%S.png')

e.g with set -xv & echo enable

$ echo $(date +'screenshot-%Y%m%d-%H%M%S.png')
echo $(date +'screenshot-%Y%m%d-%H%M%S.png')
++ date +screenshot-%Y%m%d-%H%M%S.png
+ echo screenshot-20170716-131720.png
screenshot-20170716-131720.png
igiannak
  • 750
1

You can always use command-substitution as the other answer suggests, or use xargs to use what pipe gives:

$ date '+screenshot-%y%m%d-%H%M%S.png' | xargs -I {} import  {}                                                              

$ ls screenshot-*                                                                                                            
screenshot-170716-042853.png