Tag Info

Hot answers tagged

11

eval "$1" executes the command in the current script. It can set and use shell variables from the current script, set environment variables for the current script, set and use functions from the current script, set the current directory, umask, limits and other attributes for the current script, and so on. bash "$1" executes the command in a completely ...


8

This facility is being done by what's called Bash Completion. The files that back this are stored under /etc/bash_completion.d with each command having its own file. So in the git case: /etc/bash_completion.d/git If you look at this file you'll notice that it's overloading your environment with extra functions. One in particular is this guy: $ ...


8

The most important difference between bash -c "$1" And eval "$1" Is that the former runs in a subshell and the latter does not. So: set -- 'var=something' bash -c "$1" echo "$var" OUTPUT: #there doesn't seem to be anything here set -- 'var=something' eval "$1" echo "$var" OUTPUT: something I have no idea why anyone would ever use the ...


4

Substitution is almost synonymous with expansion in this context because their meanings overlap. Neither is quite a complete sub category of the other, although in the GNU Manual section you reference there are substitutions that are considered as part of an overall expansion. An expansion is extracting the value of an identifier. E.g., if this=that, when ...


4

With this approach (function running in a subshell) you aren't going to be able to update the master shell process's state without going through contortions. Instead, arrange for the function to run in the master process. The value of the PROMPT_COMMAND variable is interpereted as a command which is executed before printing the PS1 prompt. For PS2, there's ...


4

To get the same output you note in your question, all that is needed is this: PS1='${PS2c##*[$((PS2c=0))-9]}- > ' PS2='$((PS2c=PS2c+1)) > ' You need not contort. Those two lines will do it all in any shell that pretends to anything close to POSIX compatibility. - > cat <<HD 1 > line 1 2 > line $((PS2c-1)) 3 > HD line ...


4

You can use bash extended globbing for this: shopt -s extglob DIR_UPLOADS=/home/html/wp-content/uploads/ cd ${DIR_UPLOADS} for dir in $PWD/+([0-9])/+([0-9]); do cd "$dir" && for file in *; do echo 'Compress Image' done done From the man page: +(pattern-list) Matches one or more occurrences of the given patterns So putting a ...


3

I don't know anything about Java, but I can show you a proof of concept. Say we have localfile.txt: Here is the local file. and on the remote machine, we have remote.sh: #!/bin/bash cat /dev/stdin Note that the script on the remote machine invokes a program which reads from stdin. Then pass the contents of localfile.txt to your ssh command: ...


2

set -- arg arg2 echo ${2+"$1"} #OUTPUT arg shift echo ${2+"$1"} #OUTPUT #there doesn't seem to be anything here I think the difference is generally too minimal to be worthy of notice - and the terms are often used interchangeably. Though, if you look at the above two cases, you can see that in the first example we substitute $1 for $2 as a result of ...


2

What you are looking at here is called programmable completion. On Debian/Ubunutu based systems, packages will often install a file to /usr/share/bash-completion/completions which provides the programmable completion for the command. On other distributions the /etc/bash_completion.d directory may be used (this location is deprecated on Debian/Ubuntu, but ...


2

_fn() { set -- "$@" $(cat) while ${1+:} false ; do echo "$1" && [ "$1" = "arg2" ] && echo "$1" $YOUR_CHK shift done } echo "arg2" | _fn "arg1" OUTPUT arg1 arg2 arg2 That handles both cmd-line args and stdin. It only runs the while loop to check them while you still have at least one argument saved in ...


2

I did a quick test: time bash -c 'for i in {1..10000}; do bash -c "/bin/echo hi"; done' time bash -c 'for i in {1..10000}; eval "/bin/echo hi"; done' (Yes, I know, I used bash -c to execute the loop but that should not make a difference). The results: eval : 1.17s bash -c : 7.15s So eval is faster. From the man page of eval: The eval utility ...


2

Obviously, use another symbol instead of %. Use for example -I @. So it will be: ( cd "${SOURCE_DIR}" && find . -type f -name "${FILE_GLOB}" ) | xargs -n 100 | xargs -I @ sh -c '{tar -C "${SOURCE_DIR}" --files-from - @ -cf "${DESTINATION_DIR}/$(date '+%Y%m%d%H%M%S%N')-$(uname -n).tar~"}'


2

It's a bit I/O-intensive, but you'll need to use a temporary file to hold the value of the count. ps_count_inc () { read ps_count < ~/.prompt_num echo $((++ps_count)) | tee ~/.prompt_num } ps_count_reset () { echo 0 > ~/.prompt_num } If you are concerned about needing a separate file per shell session (which seems like a minor concern; ...


2

I think I would've done this using find but just to help answer your scripting questions I've modified your example slightly. #!/bin/bash for d in *; do # First level i.e. 2014, 2013 folders. regx='^[0-9]+$' # Regular Expression to check for numerics. echo "dir: $d" if [[ $d =~ $regx ]]; then # Check if folder name is ...


1

You can not use a shell variable this way and you already understand why. A subshell inherits variables exactly the same way a process inherits its environment: any changes made apply only to it and its children and not to any ancestor process. As per other answers the easiest thing to do is stash that data in a file. echo $count > file ...


1

If you just want to edit a file that looks like what you posted, use something like perl -i -pe 's/report.lookback.days.to.retrieve.data=\K\d+/$&+1/e' file Explanation The -i enables in-place editing, so the changes are saved to the original file, -p means "print each line after executing whatever script is givven by -e"/ The s/// is the ...


1

If you just want to edit the file in place each day, which you probably don't want to do, you could do something like this: $ echo 'report.lookback.days.to.retrieve.data=61' \ | awk -F= ' /report.lookback.days.to.retrieve.data/{ printf("%s=%d\n", $1, $2+1) } ' report.lookback.days.to.retrieve.data=62 But that would ...


1

Please try reading some documentation before posting here. What you're asking is trivial to find with a 5 minute google search. You might want to read through our Help page on how to ask questions to get a better idea of how this site works. Anyway, you can either pass variables as arguments using the -v option: awk -F'[]]|[[]' -v var1="2014-04-07 23:00" ...


1

The problem is that the shell redirection (<) sends the file over the ssh tunnel. And the Java class is expecting not the file, but a string with the "filename" of a local file that will be read with a FileReader. Instead of passing the filename to the FileReader, read from the standard input. InputStreamReader isReader = new ...



Only top voted, non community-wiki answers of a minimum length are eligible