Tag Info

New answers tagged

1

Define common configuration possibilities (--options, environment variables) and adapt the scripts. It's useful for future scripts to also define script templates for those languages which have the parsing for this common stuff.


4

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 ...


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 ...


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 ...


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...


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

Add single quotes to \ , so you end up with '\'': sed -e 's# @driver.find_element(:xpath, "//a\[contains(@href,'\''##' temp_spec.rb


1

Looks like this makefile includes two files (xyz.mak and pqr.mak) from its parent directory. That seems certainly useful to me.


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 ...


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).


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 ...


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 ...


0

What version of Solaris? If 10 or newer, you might want to look into fmtopo and see if any of it's options will do what you need them to. (/usr/lib/fm/fmd/fmtopo) I'm not sure it provides the level of detail you need, though, as it won't enumerate WWNs, to the best of my knowledge.


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").


-2

The spaces in the values can be avoided by as simple for loop construct for CHECK_STR in `ls -l /root/somedir` do echo "CHECKSTR $CHECK_STR" done ls -l root/somedir contains my file with spaces Output of above my file with spaces to avoid this output, simple solution (notice the double quotes) for CHECK_STR in "`ls -l /root/somedir`" do echo ...


0

Now, with what objects are you dealing in a shell most of the time? It's files/directories, processes and their interaction. So it should like f1.edit or something like currentFile=f1.c ; .edit ; .compile ; .run. Or d1.search(filename='*.c' string='int \*'). Or p1.stop, p1.bg. That's my understanding of an ooshell.


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 ...


1

Case 1: perl -i -ane '$n=(@F==0) ? $n+1 : 0; print if $n<=2' Case 2: perl -i -ane '$n=(@F==0) ? $n+1 : 0; print $n==2 ? "\n$_" : $n==1 ? "" : $_ '


0

Not user if this is what you intended: But a way to check all the CONNECTED hba's is this: for i in ls /dev/rdsk/*s2; do luxadm display $i 2>&1|grep WWN|awk '{print $5}'; done |sort|uniq |xargs fcinfo hba-port -l |egrep 'Model' Or to get all the part-number of a system regarding HBA cards: fcinfo hba-port|egrep 'Model'|uniq I think this ...


0

Monitoring This is a pretty wide open question. There are tons of applications that you can use to monitor the load of a server's CPU, network traffic, file I/O, etc. At the most basic level I would start by monitoring the VMs CPU load. You can monitor this in 2 ways, either within the VMs themselves or externally on the VM Hosts. Monitoring through the ...


0

You can run ps -C qemu-kvm -o pid,time,etime,args on the host regularly and check the change in CPU time (the time field). That allows you an assessment over a larger period. You may save the results from time to time in order to not lose them when a VM crashes or is shut down.


0

One of two things will be true: 1) You'll have a lot of servers 2) You won't have a lot of servers (help me if I overlooked a possibility). If you don't have a lot of servers, you'd probably be able to do the hardware inventory by hand and wouldn't be asking this. If you do have a sizeable deployment then you really should look into getting something like ...


2

You can tune cpu time and other things with ulimit, for cpu time particularly: $ ulimit -t 60 # limit to 60 seconds $ program


2

I've found something a little better, than timeout: timelimit. It has several advantages; one is that user can manually abort execution by pressing "Ctrl+C". The timelimit program is available in Debian repository.


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 ...


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 ...


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 ...


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' ...


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(): # ...


0

Instead of storing the information in flat text files, you could make use of the file system. For instance, have one file per IP address in your pool. Allocating a domain to the IP address would be linking a domain file to the IP address file. Then you know what's the current allocation for the IP address by looking at the number of links (which is an ...


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 ...


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 ...


2

1) You could write variable assignments to a file in python, and then source that in your bash script. 2) Since your word (EOF) is not quoted, all the lines of the here-document are subjected to parameter expansion. You can use this to pass stuff to the python script.


-1

I didn't understand your requirement properly.. but below script does , what you mention in expected output #!/usr/bin/env bash Server_file=servers.txt # abolute path whould be great Domain_file=domain.txt Output_file=allocation.txt declare -a d_arr=( $(< $Domain_file ) ) # domain list into array while read ip d_count do case $d_count in ...


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

if (( var_a == 0 && var_b == 0 )); then # ... fi In Bash, always use (( ... )) for arithmetic evaluations.


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 ...


0

Bash and some of its kindred shells have the convenient (( ... )) notation wherein arithmetic expressions can be evaluated. So as an answer to your third challenge, where both the repeat count and delay between each repeat should be configurable, here's one way to do it: repet=10 delay=1 i=0 while (( i++ < repet )); do echo Repetition $i sleep ...


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


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

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 ...


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 ...


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

I'd use a function: gotofile () { pushd "$(find . -name "$1" -printf "%h\n")"; } Change "." to some directory as appropriate ("~" perhaps)


4

fc -s runs the previous command again: alias cdlast='cd "$(dirname "$(fc -s 2> /dev/null)")"' Or use eval "$(history -p !!)": alias cdlast='cd "$(dirname "$(eval "$(history -p !!)")")"'


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 ?.


0

If it doesn't have to be grep/cut, you may be able to get the same information using stat. In filesystem mode, %a is number of free blocks and %s blocksize, so %a * %s is the free space in bytes, and you can do math to your liking with it to break it down to a more suitable unit of your choice, for example: echo $((`stat -f -c "%a*%s" ...



Top 50 recent answers are included