Add chsrc_run_as_x_file()

This commit is contained in:
Aoran Zeng 2025-07-14 22:35:33 +08:00
parent fbdeeb125d
commit 3e3e9aede2
No known key found for this signature in database
GPG Key ID: 8F8BA8488E10ED98

View File

@ -1375,6 +1375,92 @@ chsrc_make_tmpfile (char *filename, char *postfix, bool loud, char **tmpfilename
}
/**
* bash file.bash
*/
void
chsrc_run_as_bash_file (const char *script_content)
{
char *tmpfile = NULL;
FILE *f = chsrc_make_tmpfile ("bash_script", ".bash", false, &tmpfile);
fwrite (script_content, strlen (script_content), 1, f);
fclose (f);
// chmod (tmpfile, 0700);
char *msg = CHINESE ? "即将执行 Bash 脚本内容:" : "The Bash script content will be executed:";
chsrc_note2 (msg);
println (script_content);
char *cmd = xy_2strjoin ("bash ", tmpfile);
chsrc_run (cmd, RunOpt_Dont_Abort_On_Failure);
remove (tmpfile);
}
/**
* sh file.sh
*/
void
chsrc_run_as_sh_file (const char *script_content)
{
char *tmpfile = NULL;
FILE *f = chsrc_make_tmpfile ("sh_script", ".sh", false, &tmpfile);
fwrite (script_content, strlen (script_content), 1, f);
fclose (f);
// chmod (tmpfile, 0700);
char *msg = CHINESE ? "即将执行 sh 脚本内容:" : "The sh script content will be executed:";
chsrc_note2 (msg);
println (script_content);
char *cmd = xy_2strjoin ("sh ", tmpfile);
chsrc_run (cmd, RunOpt_Dont_Abort_On_Failure);
remove (tmpfile);
}
/**
* pwsh file.ps1
*/
void
chsrc_run_as_pwsh_file (const char *script_content)
{
char *tmpfile = NULL;
FILE *f = chsrc_make_tmpfile ("pwsh_script", ".ps1", false, &tmpfile);
fwrite (script_content, strlen (script_content), 1, f);
fclose (f);
char *msg = CHINESE ? "即将执行 PowerShell 脚本内容:" : "The PowerShell script content will be executed:";
chsrc_note2 (msg);
println (script_content);
char *cmd = xy_2strjoin ("pwsh ", tmpfile);
chsrc_run (cmd, RunOpt_Dont_Abort_On_Failure);
remove (tmpfile);
}
/**
* @param cmdline
*
* @danger Bash
*/
XY_Deprecate_This("Don't use this function")
void
chsrc_run_in_inline_bash_shell (const char *cmdline)
{
char *cmd = xy_strjoin (3, "bash -c '", cmdline, "'");
chsrc_run (cmd, RunOpt_Dont_Abort_On_Failure);
}
/**
* @param cmdline
*
* @danger PowerShell
*/
XY_Deprecate_This("Don't use this function")
void
chsrc_run_in_inline_pwsh_shell (const char *cmdline)
{
char *cmd = xy_strjoin (3, "pwsh -Command '", cmdline, "'");
chsrc_run (cmd, RunOpt_Dont_Abort_On_Failure);
}
static void
chsrc_view_env (const char *var1, ...)