0

I want to execute the VASPKIT program by using a shell script.

In the terminal, I can call the program by typing 'vaspkit'

===================== Structural Options ========================
 1)  VASP Input Files Generator    2)  Elastic-Properties
 3)  K-Path Generator              4)  Structure Editor
 5)  Catalysis-ElectroChemi Kit    6)  Symmetry Search

 ===================== Electronic Options ========================
 11) Density-of-States             21) DFT Band-Structure
 23) 3D Band-Structure             25) Hybrid-DFT Band-Structure
 26) Fermi-Surface                 28) Band-Structure Unfolding

 =========== Charge & Potential & Wavefunction Options ===========
 31) Charge & Spin Density         42) Potential-Related
 51) Wave-Function Analysis
 ====================== Misc Utilities ===========================
 71) Optical-Properties            72) Molecular-Dynamics Kit
 73) VASP2BoltzTraP Interface      74) USER interface
 91) Semiconductor Calculator      92) 2D-Materials Kit

 0)  Quit
 ------------>>

Usually, I will choose '1':

1
 ==================== VASP Input Files Options ===================
 101) Customize INCAR File
 102) Generate KPOINTS File for SCF Calculation
 103) Generate POTCAR File with Default Setting
 104) Generate POTCAR File with User Specified Potential
 105) Generate POSCAR File from cif (no fractional occupations)
 106) Generate POSCAR File from Material Studio xsd (retain fixes)
 107) Reformat POSCAR File in Specified Order of Elements
 108) Successive Procedure to Generate VASP Files and Check
 109) Check All VASP Files

 0)   Quit
 9)   Back
 ------------>>

Then enter '103' to generate the file that I want.

How to achieve this in a shell script? I thought it should be something like:

#!/bin/sh

for i in *; do cd $i;vaspkit;1;103;cd -;done

I've tried it, but it doesn't work (stoped at the first step).

y ing
  • 3
  • 2
  • 1
    As an aside, note that it's more reliable to reverse a `cd` command in a shell script by scoping it to a subshell. `for i in */; do (cd "$i" && exec vaspkit <<<$'1\n103\n'); done`, for example, avoids the need for a `cd -` -- and also, the `&&` means that `vaspkit` won't be run inside a given directory should the attempt to `cd` into that directory fail. (Using `*/` instead of `*` also means that the loop won't try to `cd` into anything that _isn't_ a directory). – Charles Duffy Sep 05 '20 at 20:31

1 Answers1

1

Text in a script is interpreted as if it's commands, not as keyboard input. So, it will require automating the input somehow. One simple way is using a here-string:

command <<< "1"

But this will only allow a single key. If you want multiple inputs it's easiest to use a string literal and separate lines of input with a newline, e.g.:

command <<< $'1\n103'

This will not work in some situations, like if the code being sent the input is run in a subshell by a parent that's taking the input for itself.

For anything more complex you can try expect.

l3l_aze
  • 357
  • 3
  • 12
  • 1
    Note the bullet point regarding questions that "have been asked and answered many times before" in the *Answer Well-Asked Questions* section of [How to Answer](https://stackoverflow.com/help/how-to-answer). – Charles Duffy Sep 05 '20 at 20:29
  • Sorry; used to Reddit & similar sites, and still trying to get a handle on this after a couple years =\\ Should I delete this "answer", or edit to point to an existing one? – l3l_aze Sep 06 '20 at 02:51
  • 1
    No need to delete or edit. There's already a pointer to preexisting instances up at the top of the question; view and upvote statistics will determine whether the question is eventually cleaned up or sticks around as a pointer to those preexisting instances. Consider the above a gentle suggestion for the future, not an indication of anything you need to do now. – Charles Duffy Sep 06 '20 at 17:03
  • 1
    (The other thing is that your answer is correct and generally in line with good practices. If it were misleading or potentially harmful in some way such that someone following the duplicate links would get substantially better advice I might advise deletion, but that's not the case here). – Charles Duffy Sep 06 '20 at 17:23