A message from our CEO about the future of Stack Overflow and Stack Exchange. Read now.
28

Modifying your original script: #!/bin/sh echo "Noise $1" echo "Enhancement $2" for snr in 0 5 10 15 20 25 do python evaluate.py --noise "$1" --snr "$snr" --iterations 1250 ${2:+--enhancement "$2"} done The standard parameter expansion ${var:+word} will expand to word if the variable var is set and not empty. In the code above, we use it to add --...


12

On a minimal POSIX shell, that does not support any arrays, you can just manipulate the positional argument list with set command. Just before the for loop you can do if [ -z "$2" ]; then set -- --noise "$1" --snr 25 --iterations 1250 else set -- --noise "$1" --snr 25 --iterations 1250 --enhancement "$2" fi and now run your for loop as for snr in ...


8

When there are multiple extensions for a destination, you could put more logic into the find directives: find ~/Downloads -maxdepth 1 \( -name "*.tar.gz" -o -name "*.zip" \) -print0 | xargs -0 -I % mv % ~/Downloads/archives/ And you don't need to pipe to xargs: find ~/Downloads -maxdepth 1 \( -name "*.tar.gz" -o -name "*.zip" \) -exec mv -t ~/Downloads/...


5

As a matter of taste, I hate duplicating code and I tend to separate data from code, so I would have some way to define an extension<->target relationship (here in a key-indexed array, but that could be a config file or an sqlite DB): #! /bin/bash declare -A destinations destinations["jpg"]="images" destinations["mp4"]="videos" destinations["mov"]="...


4

Your issue with the first command is that the * characters are unquoted. The shell will therefore expand them to any matching filenames in the current directory. Your issue with the second command is that the command contains both single and double quotes, so you can't reliably just quote the crontab entry with double quotes as you try to do. You would ...


4

You have a rather simple syntax problem. Just put spaces before and after the test construct, as in if [[ "$fn" == "01" ]]; then ... and it should work. The reason is that the [[ is acually a bash keyword, not an operator, and as such has to be placed "standalone" (similar to the if etc.). One of your files seems to be called 01. With your syntax, the ...


3

I'm not aware of any shell in which a whitespace would matter. For example, in man bash it says: In a non-interactive shell, or an interactive shell in which the interactive_comments option to the shopt builtin is enabled (see SHELL BUILTIN COMMANDS below), a word beginning with # causes that word and all remaining characters on that line to ...


3

There’s no non-stylistic difference; POSIX specifies that, when recognising tokens in the shell, If the current character is a '#', it and all subsequent characters up to, but excluding, the next <newline> shall be discarded as a comment. The <newline> that ends the line is not considered part of the comment.


3

find is able to do the mv command by itself, syntax to use is: find <filter> -exec mv {} <destination> Note that: {} will be replaced by filename and you sometimes need to escape it that way \{\} If you tolerate bash'ism, you can take advantages of bash arrays and rewrite your script that way: BASE_PATH="~/Downloads/" # 1st element: ...


3

How about $ awk -F': ' ' BEGIN{OFS=FS; getline value < "File2"} $1 == "keyword" {$2 = q value q} 1 ' q="'" File1 not_keyword: 'something' keyword: 'condition 1 condition 2 condition 3' another_not_keyword: 'something'


3

You can do it in a single line by using command substitution python evaluate.py --noise $1 --snr 25 --iterations 1250 $(test -n "$2" && echo --enhancement "$2")


2

Correctly quoted line should look like this: $ echo \*/2\ \*\ \*\ \*\ \*\ /usr/bin/aws\ ssm-send-command\ --document-name\ \"AWS-RunShellScript\"\ --parameters\ \'\{\"commands\":\[\"echo\ \{\{ssm:/mr2/cloudwatch-custom\}\}\"\]\"\}\'\ --instance-ids\ \"\$INSTANCE_ID\"\ --region\ us-east-1 Of course, I didn't come up with this crazy quoting by myself, I used ...


2

It doesn't matter that much in this case, because your task is idempotent, but your locking is incorrect and subject to a race condition. You check for the presence of the lockfile, and then create it, using two distinct (non-atomic) operations. Because of this, If two instances of the script are launched at the same time, it is very possible that both will ...


2

trap 'echo GOT ERROR, exiting' ERR Simply saying "exiting" doesn't mean it's also true ;-) An ERR trap will executed whenever a command fails, no matter if the script exit immediately afterwards (eg. because of set -e) or not: $ bash -c 'trap "echo error, not exiting yet" ERR; false; echo DONE' error, not exiting yet DONE In your case the command which ...


2

Build the argument in a variable based on the input: #!/bin/sh echo "Noise $1" echo "Enhancement $2" if [ $# -eq 1 ] then enhance="" else enhance="--enhancement $2" fi for snr in 0 5 10 15 20 25 do python evaluate.py --noise $1 --snr "$snr" --iterations 1250 $enhance done (Also includes the correction of the loop variable in the body of the loop)


1

try breaking into two string echo "*/2 * * * *" /usr/bin/aws ssm-send-command (...) >> /etc/cron.d/lvm_disk_space this way * won't be expanded to local filenames, and you get remaining argument. Note that external quote are removed. e.g. --document-name "AWS-RunShellScript" is expanded as --document-name AWS-RunShellScript


1

So I kept playing with this script, and it felt like I was aiming at a moving target. I wasn't keeping super close track of my changes and what-not, so there may have been other issues going on. However, I think I've fixed my script to work correctly. The goal of this script, is to take an arbitrary source directory, that contains *.flac and *.jpg files,...


1

You can put in this script in your monitorSerialPortsLauncher #!/usr/bin/env bash script=/tmp/monitorSerialPorts cat << 'EOF' > $script #!/usr/bin/env bash sleep 10 while true do clear printf "\n Serial Devices: " ls /dev/ttyUSB* 2> /dev/null || (clear ; printf "\n No Devices Detected") sleep 2 done EOF chmod 755 $script gnome-terminal -...


1

This is a solution that relies solely on leading space(s). It doesn't rely on expected number of fields. The manual of GNU sed provides a way to "join lines that start with whitespace": sed -E ':a ; $!N ; s/\n\s+/ / ; ta ; P ; D' The manual claims a portable (non-GNU) variation is: sed -e :a -e '$!N;s/\n */ /;ta' -e 'P;D'


1

In fact in your first example of alias call you made after expansion: cd test ---> cd $1 && ls test It is the basic difference between the bash script call and the expansion of an alias! By use of an alias your parameter is written after all the characters of the alias definition. The $1 is used literally and it is not substituted with the word ...


1

This works for me, using date (GNU coreutils) 8.26. $ date -d "5 minutes ago" +%Y%m%d%H%M%S 20200120101533


1

It might work like this. past=$(date +%Y%m%d%H%M%S -d ${now} -d "-5minutes")


1

It seems GNU date cannot parse timestamps in that format. It takes the leading portion to be a year: % date +%Y%m%d%H%M%S -d 20200120175610 --debug date: parsed number part: (Y-M-D) 2020012017-56-10 date: input timezone: system default date: warning: using midnight as starting time: 00:00:00 date: error: invalid date/time value: date: user provided time:...


1

#!/bin/sh MIN=1 MAX=11 PADDING=4 for i in $(seq -f "%0${PADDING}g" $MIN $MAX); do file=${i}.txt printf "$file\n" touch $file done printf "\n\nFINISHED!\n\n"


1

You must be using the GNU implementation of date where that 20200101101010 is interpreted as 2020010110-10-10T00:00:00 local time. -d is a non-standard option of date, supported by only a handful of implementations and on those where it's supported, if it is to input a date at all (like in GNU, busybox or ast-open's date; BSD date using -r and -v instead), ...


1

Your script breaks because you are using $var unquoted on the command line with egrep. This causes the shell to split it into separate words on spaces, tabs and newlines (by default), and each word is also undergoing filename globbing (e.g. snmp.* would be expanded to all matching filenames in the current directory) before egrep is called with the generated ...


2

Your script is launched in a new non-interactive shell that is forked from your current shell (interactive). Any changes made on the new spawned shell are reflected only for the lifetime of the script. So in your case, the cd to the new path is reflected only in the new shell and will be not be reflected back to the parent shell because the spawned shell ...


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