17

The [[ ... ]] syntax isn't valid for /bin/sh. Try: if [ -e /usr/src/an-existing-file ] then echo "seen" >> /etc/rclocalmadethis fi Note that sometimes it works because /bin/sh -> /bin/bash or some other shell that supports that syntax, but you can't depend on that being the case (as you see here). You can run ls -l /bin/sh to get to ...


11

You are calling your bash script with sh. This is usually a basic POSIX shell like dash. The [[ isn't POSIX, it's a bashism (also present in some other shells) so your sh doesn't support it: $ dash -c "if [[ 10 -gt 8 ]]; then echo yeah; fi" dash: 1: [[: not found So, either change your script to use the standard [ (you're not using any special ...


7

In sh and compatible shells the exit status from a nonexistent command should be 127. If a command is not found, the exit status shall be 127. If the command name is found, but it is not an executable utility, the exit status shall be 126. (source) The command builtin doesn't change much: […] the following exit values shall be returned: 126 The utility ...


7

Try with read command to get runtime the input from the user. Since it has timeout option. From man: -t timeout Cause read to time out and return failure if a complete line of input is not read within timeout seconds. timeout may be a decimal number with a fractional portion following the decimal point. This option is only effective if read is ...


6

type to command line this: !echo


5

Using reverse interactive search: hit Ctrlr start typing for the command you want when the shell has found the one you want, hit Enter. If you want to use a previous echo command hit Ctrlr type echo hit Ctrlr again, repeatedly until you find the command you want. if you go too far back in history, Ctrls searches in the opposite direction. If you don't ...


5

The traditional answer, for shells that has arrays, is to use an array: arg=( "$@" ) You then have $1 in ${arg[0]}, $2 in ${arg[1]} etc. To loop over these, use for a in "${arg[@]}"; do # code that uses "$a" goes here done or, if you still have the data in the list of positional parameters and want to reduce typing, for a ...


5

Please read bash tutorials and manual on using conditional expressions, e.g. [ ] and test. In short: There's no need to use ==, as = is enough. You must put spaces before and after = because otherwise instead of comparing you're assigning the right part of the expression to the left one. Do quote your variables The net result: if [ "${name}" = &...


4

You can copy with the wildcards, that's fine. However, you need to rename the files, not just copy them which means you have to assign a new name to each file and that means a loop is unavoidable. At best you can use a tool that does the looping for you. If you have perl-rename (called renamed on Debian-based systems), you can do: $ rename -n 's|raw/([^/]+)/....


4

This is because you are not resetting OPTIND. According to the manual: Each time it is invoked, getopts places the next option in the shell variable name, initializing name if it does not exist, and the index of the next argument to be processed into the variable OPTIND. OPTIND is initialized to 1 each time the shell or a shell script is invoked. So ...


4

Your attempt with sleep would not work as the sleep call and the subsequent test on $flag_cancel happens in a background job. Any change to the variable flag_cancel in the main part of the code would not affect the value of the variable in the backgrounded subshell and the code would unconditionally suspend the system after 10 seconds. Instead, you can use ...


3

1st line tries to execute /sys/.../temp. That is not what you want. Use cat or head -n 1 to retrieve the value in that file. 2nd line lacks the $() construct to evaluate the command and store its output. 3rd line complains about permission. You need write permission to the temp.log file, which can be granted with chmod ugo+w temp.log. I have also replaced ...


3

Fish is not bash compatible, but uses its own scripting language. In this case the only differences are it doesn't support backticks (```), instead it uses parentheses. for-loops don't use do/done, instead they just end in "end" for acc in (cat uniprot_ids.txt); curl -s "https://www.uniprot.org/uniprot/$acc.fasta" ; end > ...


3

[[ is a bash feature not available in sh: root@d4b4b6325f2a:/# type [[ [[ is a shell keyword root@d4b4b6325f2a:/# sh # type [[ [[: not found


3

With zsh: autoload -Uz zmv # best in ~/.zshrc zmv -C 'raw/(*)/*.pdf' 'pdf/$1.pdf'


3

Use nested loops: shopt -s nullglob # To prevent failures if there's no pdf in a dir. for dir in raw/* ; do for pdf in "$dir"/*.pdf ; do cp -- "$pdf" pdf/"${dir#raw/}".pdf # Remove the "raw/" part done done or iterate over the files and apply the parameter expansion twice to generate the target name: ...


3

Using GNU Parallel you can do: parallel --line-buffer --tag 'dvdbackup -Mv --progress --error=a -i /dev/{1} -o /home/xxxx/vids/' ::: sr0 sr1 From its manual: --tag     Tag lines with arguments. Each output line will be prepended with the arguments and TAB (\t).


2

You can't. The programs are running in parallel, so they are both writing to the terminal at the same time. What you can do is capture the standard error of each instance into a separate file: dvdbackup -Mv --progress --error=a -i /dev/sr0 -o /home/xxxx/vids/ 2>log1 & dvdbackup -Mv --progress --error=a -i /dev/sr1 -o /home/xxxx/vids/ 2>log2 You ...


2

Execute this as bash scriptname /path/to/files: #!/bin/bash for f in "$1"/*.{jpg,png}; do [ -f "$f" ] || continue base=$(basename "$f") convert -resize 60x60 "$f" "$1/60/${base%.*}-60.${base##*.}" done Add more extensions to the braces if needed. convert takes an input file $f and places the result of ...


2

Let's start with the easy bit When I do sh testenv.sh [...] That's wrong. You've written a bash script, and even declared it as such, so call it with bash not with sh. More of the same In the file .myenv you show that you have #!/usr/bin/env sh source ./testenv.sh so you're declaring that this is a sh script (not a bash script), but then you're using ...


2

In general, an alternative you have is timeout from GNU coreutils. Not the best choice here, because we are setting a timeout for a builtin command and an external utility can only do it by spawning a whole shell: if ! timeout 10 bash -c ' select conf in yes no do case $conf in (yes) exit 1;; (no) exit 0;; esac done' then echo '...


1

You can use the t option of the read command. For example; read -t 5 -p "Input within 5 seconds:" data if [[ $data ]] then echo " Your input is $data" else echo " You didn't any input" fi Run script; If no give any input within 5 seconds; #~ bash scr.sh Input within 5 seconds: You didn't any input If give ...


1

The traditional answer is to use eval. for ((i=1; i<=$#; i++)) do eval "arg$i=\${$i}" done (note that the braces are required as in bash, $10 would be interpreted as ${1}0 instead of ${10})


1

command < file will redirect content of file to command to redirect output from executable (in your case execution of file.sh ) use ./file.sh | ./program note that | is called pipe see man bash §redirection


1

There are a couple approaches you can take here. The first, and easiest, is to use a tool like tmuxinator to handle your tmux configuration if you always want the same thing. You can also script tmux from the command line. For example, you could do this to create a single new session with a new window with a 4-way split: #!/bin/sh tmux source-file <<...


1

There's a bug in your script. Arguably there's also a bug in rsync, because it's silently doing something surprising when you pass it weird data, whereas it would be better for it to error out. But either way your script doesn't work. The problematic line is: rsync -auv "$3" "$1" "$2" When the third argument of the script (&...


1

#!/bin/bash python3 print("Hello World") exit() echo "The execution is completed" Scripts work differently from typing commands directly in a terminal, ie. what you want to do won't work this easily. The #!/bin/bash line tells the kernel that the program it's trying to start is a script that need to be run with /bin/bash. So when you run ...


1

If you want to have something like a "shell script template" for how to loop over directory contents and perform actions based on the file type, the following might give you a hint: for f in * do if [[ -d "$f" ]] then ... your processing of directories here, reference them as "$f" ... fi done This would look over ...


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