Hot answers tagged scripting
12
If you have rsync (remove --dry-run to do it for real):
rsync --dry-run --remove-source-files -avHAX /unencrypted/ /encrypted
Otherwise, using bash4+ and GNU stat:
#!/bin/bash
set -e
shopt -s nullglob globstar
for from in /unencrypted/**/*; do
to="${from/\/un//}"
if [[ -d "$from" ]]; then
echo mkdir -p "$to"
echo chmod "$(stat ...
6
Getting a variable to Python
Since variable substitution occurs before text is passed from the heredoc to python's standard input, you can throw the variable right in the script.
python - <<EOF
some_text = "$some_text"
EOF
If some_text was "test", python would see some_text = "test".
If you want to be able to pull your Python code right into a ...
5
ScriptB has CRLF line endings. Since the kernel doesn't support CRLF, it's trying to execute a program named "/path/to/bin/perl\r" (where \r indicates a CR). That program doesn't exist. Convert ScriptB to LF line endings. (Thank Stephane Chazelas for asking the right question. The \r in the sed output indicates the problem.)
sed l\;q FILE runs 2 sed ...
5
Linux (you mentioned "only under Ubuntu" but the only OS you mentioned it working under was Darwin) does not support passing multiple arguments to a 'shebang' interpreter. It passes the entire string (in your case, "zsh -") as a single argument.
The correct way to ensure your package does not depend on the location of an interpreter is to, as part of the ...
4
I think the answer to your question is basically "no". The shebang mechanism just isn't that flexible.
The #! line only lets you specify a command to execute, and (optionally) a single argument to that command. The name of the script is passed as another argument. So if foo.zsh starts with:
#!/usr/bin/env zsh
the running foo.zsh is equivalent to ...
4
The strings inside single quotes are used verbatim by the shell (and hence cannot contain other single quote, since that would be treated as the closing one of a pair). That said, you have several options:
end the single-quoted string, add escaped (either by backslash or double quotes) single quote, and immediately start the following part of your string:
...
3
You could have your sed script start with this instead:
\:;s=sed;type gsed >/dev/null 2>&1 && s=gsed; exec "$s" -f "$0" "$@";$:s/^//
That's a no-op in sed (though would slightly degrade your script performance) and when interpreted by a shell would execute with gsed if found or sed if not (both looked up in $PATH).
3
The problem with your approach is that the embedded python script no longer has access to the original stdin (since its stdin is... itself).
If that's an issue you can write:
python -c '
import sys;
for r in range(3):
print r
for a in range(2):
print "hello"
'
Or if the python script may contain single quotes:
python -c "$(cat << 'EOF'
...
3
Use a dash as the filename:
ruby - a b <<'END'
puts ARGV.join(",")
END
python - a b <<'END'
import sys
print ",".join(sys.argv[1:])
END
I don't know if sys.argv[1:] is the right way to do this in Python. For -e / -c you can specify end of arguments with --:
set -- -a -b -c
ruby -e 'puts ARGV.join(",")' -- "$@"
python -c 'import sys; print ...
3
Potential solution #1
Use the timeout command:
$ date
Mon May 6 07:35:07 EDT 2013
$ timeout 5 sleep 100
$ date
Mon May 6 07:35:14 EDT 2013
You can put a guard into the timeout command as well to kill the process if it hasn't stopped after some period of time too.
$ date
Mon May 6 07:40:40 EDT 2013
$ timeout -k 20 5 sleep 100
$ date
Mon May 6 ...
3
You can use an alias.
alias cdlast='cd "$(dirname "$(eval $(history -p !!))")"'
Make sure you have the double quotation marks in there. Those prevent the results of the command substitutions from being split into separate words if they have spaces and interpreted as wildcard patterns if they have special characters such as * and ?.
3
You want to use dirname $file to get the directory name of your input file and prepend that to the output filename.
for file in $(find ./ -name "*.nii")
do
rawdata = $(dirname $file)/rawdata.nii
fslroi $file $rawdata 0 33
gunzip $rawdata.gz -f
fslroi $rawdata rawnodif 0 1
bet rawnodif rawnodif_brain -m -g 0.2 -f 0.3
fslmaths rawnodif -mas ...
3
Shell globbings are expanded in lexical order by default. If you need a different sort order, you'll need a shell that supports specifying the order like zsh which is probably a good thing since you're already using zsh syntax there (by not quoting $f).
for f in ./jobqueue/*(.NOm); do
chmod +x $f
$f
done
The (.NOm) part is zsh's globbing qualifiers. ...
2
You need to have a look at the man pages for proc. Look at /proc/[pid]/*
entries and pick which files you want.
You do not want to copy everything. As an example you have
/proc/[pid]/mem which is all the virtual memory for a process including
shared etc. – i.e. size of all your memory. Further you are not able to read it unless the process own mem or it is ...
2
Provided that your filenames don't contain spaces or tabs or newlines or ? or * or [ and that the directory doesn't contain subdirectories, you might try something like
for f in $(ls -tr ./jobqueue/) ; do
chmod +x ./jobqueue/$f
./jobqueue/$f
done
2
Thank you, here is the final script. Note the $ denotes a filename, whilst the rest are commands or arguments.
#!/bin/bash
for file in $(find ./ -name "*.nii")
do
rawdata=$(dirname $file)/rawdata.nii.gz
rawnodif=$(dirname $file)/rawnodif.nii.gz
rawnodif_brain=$(dirname $file)/rawnodif_brain.nii.gz
rawnodif_brain_mask=$(dirname $file)/rawnodif_brain.nii.gz
...
2
The program comes with its own dynamic loader. It's quite rare for programs to need their own dynamic loader: usually the one on your system will work too. This may be necessary if the program was linked against a standard library other than GNU libc or if it was linked against a GNU libc compiled with strange settings.
It may be enough to tell the loader ...
2
You don't need much bash code to implement classes or objects in bash.
Say, 100 lines.
Bash has associative arrays that can be used to implement a simple Object system with inheritance, methods and properties.
So, you would might define a class like this:
class Queue N=10 add=q_add remove=q_remove
Creating an instance of this Queue might be done like ...
2
There are a few options:
Use #!/usr/bin/gsed -f (assuming it is in /usr/bin) as the shebang everywhere, and make sure that your Linux environments symlink this properly;
Remove the GNUisms;
Symlink sed to /usr/bin/gsed from a directory that earlier than /usr/bin in the user's $PATH (possibly dangerous);
Make a wrapper script that looks something like this:
...
2
It looks like your array syntax is off just a bit. Also, there's no need for the index variable; you can use the += operator to append to an array.
#!/bin/bash
FILENAME=$1
rutaServ=()
while read LINE
do
rutaserv+=($LINE)
echo "ruta -> $LINE"
done < "$FILENAME"
bash v4 has a new command, mapfile (or readarray) to read the contents of a file into an ...
1
bash-[41]$ touch myscript.sh
bash-[42]$ chmod +x myscript.sh
bash-[43]$ echo '#!/usr/bin/env bash' > myscript.sh
bash-[44]$ echo 'mv plugin-cfg.xml plugin-cfg2.xml' >> myscript.sh
bash-[45]$ echo 'mv plugin-cfgbk.xml plugin-cfg.xml' >> myscript.sh
bash-[46]$ ./myscript.sh
EDIT FOR CLARITY
Above was intended to be a simple answer to a ...
1
That syntax error there seems to be hinting at unescaped regex. I personally don't use sed that often, but if I remember correctly, you'll also need to escape things like parenthesis (and with double backslashes if you are using double quotes). Here is a list of possible characters that you may need to escape...
1
POSIX doesn't provide a way to get the last PID assigned by the kernel so there can't be a portable answer.
Here is a oneliner that should work on all systems implementing dtrace (Solaris, FreeBSD, NetBSD, Mac OS X, Oracle Linux with latest UEK, and others like Illumos based OSes, Linux with dtrace4linux)
# dtrace -qn 'proc:::exec-success { printf("%Y - ...
1
This saves the timestamp and last PID to file every second in Bash:
if [ -r /proc/sys/kernel/ns_last_pid ]
then
while true
do
while read
do
if [ "$REPLY" != "$old" ]
then
printf '%(%s)T %d\n' -1 "$REPLY"
old="$REPLY"
fi
done < /proc/sys/kernel/ns_last_pid
...
1
(Giving an answer to my own question, or rather, an alternative approach that will work in this situation.)
A sed script ascript.sed, even when they start with a sha-bang, can also be applied from the command line on a target file target.txt as sed -f ascript.sed target.txt. This means that when on Mac OS X, one can call the script as gsed -f ascript.sed ...
1
Could the script be called like script.sh process1 process2? Then you could refer to the arguments with $@:
# set process1 process2 # set arguments for testing
for x in "$@"; do
echo "ruta -> $x"
done
[rutaServ$i]=$LINE should be rutaServ[$i]=$LINE or rutaServ+=("$LINE").
1
To get this to work I would suggest two major changes:
use public/private keypairs
send commands on the commandline to ssh
You can use ssh-keygen to generate a new private-public key pair and use ssh-copy-id to install the public key you generate to the new account on 172.0.0.2.
After that you don't have to go through the expect "123" sequence anymore.
...
1
The following python script should do what you want:
#!/usr/bin/python
serv=[]
for l in open("servers.txt","r").xreadlines(): # for each server
s,n = l.split(",") # extract server name and load
n=int(n.split("#")[0].strip()) # ignore comments
serv.append([s.strip(),n]) # store server and its load
for l in open("domain.txt","r").xreadlines(): # ...
Only top voted, non community-wiki answers of a minimum length are eligible

