Tag Info

New answers tagged

1

If you want the entire script to run as another user, my usual technique for doing this is adding something similar to the following to the very top of the script: target_user="foo" if [ "$(whoami)" != "$target_user" ]; then exec sudo -u "$target_user" -- "$0" "$@" fi Note that I use sudo here and not su. su makes it stupidly difficult to pass arguments ...


0

By putting the EOF in quotes, you are effectively quoting the "here document" (echo $1), such that $1 is interpreted by the user2 shell.  But that shell doesn't have any positional parameters.  I can't test these right now, but here are a couple of approaches that might work: Don't quote EOF: sudo su user2 << EOF echo $1 EOF Pass values through the ...


5

POSIXly, the parsing for options should stop at -- or at the first non-option (or non-option-argument) argument whichever comes first. So in cp -R file1 -t /mybackup file2 -f that's at file1, so cp should recursively copy all of file1, -t, /mybackup and file2 into the -f directory. GNU getopt(3) however (that GNU cp uses to parse options (and here you're ...


1

So every time getopts processes an argument it doesn't expect it sets the shell variable $OPTIND to the next number in the argument list which it should process and returns other than 0. If $OPTIND is set to a value of 1, getopts is POSIX-specified to accept a new argument list. So this just watches getopts return, saves increments a counter plus $OPTIND's ...


0

The operating system's argument passing limit does not apply to expansions which happen within the shell interpreter. So in addition to using xargs or find, we can simply use a shell loop to break up the processing into individual mv commands: for x in *; do case "$x" in *.jpg|*.png|*.bmp) ;; *) mv -- "$x" target ;; esac ; done This uses only POSIX Shell ...


1

if [ $@ = "ffki" ]; then The command in the if clause expands to [ followed by the list of wildcard expansions of the words that make up the positional parameters followeb by the words = ffki ]. Generally speaking, $VAR does not mean “the value of VAR” when it's outside double quotes, it means “split the value of VAR into separate words and interpret ...


0

You probably want something like this: # getopt code that sets "$c_value" goes here ... # ... then if [[ -z $c_value ]] && (( $# > 0 )); then c_value=$1 shift fi


1

I added right at the beginning of this script: if [ $# = 1 ]; then # If there is only one argument, replace the current process with the new invocation of the script # the only option will be used as -c option exec "$0" -c "$@" fi This doesn't answer the initial question, but it a workaround, that works in my special case.


1

You can do something like if [ $# = 1 ]; then # do whatever you'd do when there is only one argument else # do the getopt bits fi # do what you'd do in either case If -c is the only switch you want to support, then you don't need getopt and can instead do something like this: #!/bin/bash usage() { echo "Usage: $0 [ -c ] arg" >&2 ...


2

In addition to @Michael Homer's answer, you can use bash eval function: PARMS='-rvu' PARMS+=" --delete --exclude='.git'" echo "$PARMS" eval "rsync ${PARMS} . "'"${TARGET}"'


12

There is a difference between: PARMS+="... --exclude='.git'" and ... --exclude='.git' In the first, the single quotes are inside quotes themselves, so they are literally present in the substituted text given to rsync as arguments. rsync gets an argument whose value is --exclude='.git'. In the second, the single quotes are interpreted by the shell at ...


0

your script would look something like: #!/bin/bash /path/to/timidity "$1" But, can you right-click the midi file and "open with" timidity instead? Seems simpler.



Top 50 recent answers are included