'-h'|'--help'|'-V'|'--version')
return 0
;;
+ '-c'|'--command')
+ COMPREPLY=( $(compgen -c -- $cur) )
+ return 0
+ ;;
esac
case $cur in
-*)
- OPTS="--version --help"
+ OPTS="--version --help --command"
COMPREPLY=( $(compgen -W "${OPTS[*]}" -- $cur) )
return 0
;;
esac
- COMPREPLY=( $(compgen -g -- $cur) )
+ if (( COMP_CWORD == 1 )) || [[ " ${COMP_WORDS[@]}" =~ " "-?-c ]]; then
+ COMPREPLY=( $(compgen -g -- $cur) )
+ else
+ COMPREPLY=( $(compgen -c -- $cur) )
+ fi
return 0
}
complete -F _newgrp_module newgrp
== SYNOPSIS
-*newgrp* [_group_]
+*newgrp* [_group_ [_command_]]
+
+*newgrp* [*-c* _command_] [_group_]
== DESCRIPTION
If no group is specified, the GID is changed to the login GID.
+An optional command can be specified, which is invoked after group change instead of the user's shell.
+
== OPTIONS
+*-c*, *--command*=__command__::
+Pass _command_ to the user's shell with the *-c* option.
+
include::man-common/help-version.adoc[]
== FILES
{
FILE *out = stdout;
fputs(USAGE_HEADER, out);
- fprintf(out, _(" %s <group>\n"), program_invocation_short_name);
+ fprintf(out, _(" %s <group> [[-c] <command>]\n"), program_invocation_short_name);
fputs(USAGE_SEPARATOR, out);
- fputs(_("Log in to a new group.\n"), out);
+ fputs(_("Log in to a new group; optionally executing a shell command.\n"), out);
fputs(USAGE_OPTIONS, out);
fprintf(out, USAGE_HELP_OPTIONS(16));
{
struct passwd *pw_entry;
struct group *gr_entry;
- char *shell;
+ char *shell, *command = NULL;
int ch;
static const struct option longopts[] = {
{"version", no_argument, NULL, 'V'},
{"help", no_argument, NULL, 'h'},
+ {"command", required_argument, NULL, 'c'},
{NULL, 0, NULL, 0}
};
textdomain(PACKAGE);
close_stdout_atexit();
- while ((ch = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1)
+ while ((ch = getopt_long(argc, argv, "c:Vh", longopts, NULL)) != -1)
switch (ch) {
+ case 'c':
+ command = optarg;
+ break;
case 'V':
print_version(EXIT_SUCCESS);
case 'h':
if (!(pw_entry = getpwuid(getuid())))
err(EXIT_FAILURE, _("who are you?"));
- if (argc < 2) {
+ if (argc <= optind) {
if (setgid(pw_entry->pw_gid) < 0)
err(EXIT_FAILURE, _("setgid failed"));
} else {
errno = 0;
- if (!(gr_entry = getgrnam(argv[1]))) {
+ if (!(gr_entry = getgrnam(argv[optind++]))) {
if (errno)
err(EXIT_FAILURE, _("no such group"));
else
fflush(NULL);
shell = (pw_entry->pw_shell && *pw_entry->pw_shell ?
pw_entry->pw_shell : _PATH_BSHELL);
- execl(shell, shell, (char *)NULL);
+ if (!command && optind < argc)
+ command = argv[optind];
+ execl(shell, shell, command ? "-c" : NULL, command, (char *)NULL);
errexec(shell);
}