Tag Info

New answers tagged

1

@dhag has a great answer. You can also use: a=0 while [ "$a" -eq 0 ]; do ... [Nn]*) a=1; ;; ... done


1

In the case where the user entered "y", you can exit both while and case: break [n] Exit from within a for, while, until, or select loop. If n is specified, break n levels. n must be ≥ 1. If n is greater than the number of enclosing loops, all enclosing loops are exited. The return value is 0 unless n is not greater than ...


0

It depends on the content of the shell script and the used shell. A shell script might contain boilerplate code like the following #!/bin/bash If you're using a shell that supports interpretation of this boilerplate - probably you are - the bash shell will be used as the interpreter, though your interactive shell session might be in another interpreter, ...


2

The ./foobar.sh file is started by running it with whatever follows the #! in the first line. If this line reads #!/bin/sh -x then it would be identical to the sh -x foobar.sh case (assuming sh is resolved to /bin/sh from the PATH). Maybe it is not started by sh but bash? The -x flag prints debug info, i.e. every command before it is executed.


0

The first version, with an explicit call to sh will run your script with sh (displaying trace statements, as requested by -x). The second one, which does not specify an explicit interpreter, will respect the shebang line, if any, or default to the shell you are currently running. For example, if your script has a shebang line of #! /bin/bash, or your shell ...


1

You can use find ... -exec option: find . -name iaGambro_gambro.sql -exec bash -c ' select file do : Do something with "$file" break done ' bash {} +


0

If you have xjobs installed, you'll find that this is exactly the kind of task it's designed for. In particular, check the description of the -j option to tune how many processes are created.


2

make can be good for this. You'll need a Makefile with something like %.mp3: %.wav lame $(LAMEOPTS) $< $@ Then generate a list of targets: $ find . -name '*.wav' -type f | sed -e 's/\(.*\.\)wav$/all: \1mp3/' >>Makefile Now, tell make to schedule the tasks: $ make -r -j4 -f Makefile


1

On a GNU system: find "$1" -mindepth 1 -maxdepth 1 -prune -printf '%s\0 %f\n\0' | awk -vRS='\0' ' {getline file} {n++; sum += $0; files[$0] = files[$0] file} $0 < min || n == 1 {min = $0} $0 > max || n == 1 {max = $0} END { if (n) printf "min: %d\n%s\nmax: %d\n%s\naverage: %.17g\n", \ min, files[min], ...


2

I would suggest passing the list to xargs and use the -n and/or -P options to control how many processes are running at any given time.


2

Files and directories can have newlines in their names, so you cannot "just" use wc -l as you are doing. If you want a script that takes a parameter and recursively counts files and directories, you should use: #!/bin/bash echo -n 'total directories:'; find "$1" -type d -print0 | python -c "import sys; print len(sys.stdin.read().split('\0'))-1" echo -n ...


0

You need to accept the first parameter from the command line, $1. Ideally you would assign that to a variable, DIR="$1". If nothing has been provided then it seems you would want to default to the current directory .. You can do this with test -z "$DIR" && DIR='.' or, more idiomatically just use the variable and provide a default value "${DIR:-.}". ...


1

And now for the Ubuntu answers. This is an Ubuntu Linux question, and version 15 is now released. The Ubuntu world now has systemd. But even before version 15 the Ubuntu world had upstart. There really isn't a reason to write System 5 rc scripts; and there is certainly no good reason for starting from there. Both upstart and systemd do all of the ...


1

Try this. $1 should be the parent dir containing all of your repositories (or use "." for the current dir): #!/bin/bash function git_branches() { if [[ -z "$1" ]]; then echo "Usage: $FUNCNAME <dir>" >&2 return 1 fi if [[ ! -d "$1" ]]; then echo "Invalid dir specified: '${1}'" return 1 fi ...


0

You need to introduce a file for done of while such as : done < myfile.txt


0

/ in the arithmetic expression performs an integer division, i.e. it's the quotient operator. Most shells can only do integer arithmetic, not floating point arithmetic. Instead of dividing by the total and then multiplying by 100, do the opposite. While you're at it, grep … | wc -l can be simplified to grep -c. Furthermore, grep '1.' is wrong: it selects ...


3

A shell script is a program. Essentially, a user can execute a shell with a shell script as an argument. The shell will interpret the contents (a script language) and execute the listed commands in order from top to bottom. For example, if you have bash shell (typically on linux and bsd systems) you could write a file called hello-world.sh with the ...


1

With a recent version 4.x of GNU awk you could do: awk ' BEGINFILE { count = 0 } FNR == 1 { next } $1 > 1.0 { count++ } ENDFILE { if (count/(FNR-1) >= 0.1) printf "mv %s positive_COGs\n", FILENAME } ' *.omega | sh It initializes the counters (BEGINFILE), skips the header lines (FNR == 1), counts according to the numbers found in the data ...


3

You should not be using exit inside your for loops - this causes the script to exit, and is why you're only getting one result. You should be using continue, which will stop the current loop from going on, but will go to the next element in the for loop. Swap both your exit statements to continue and you should find very different behavior, more in line ...


-1

CTRL+ALT+V or ESC CTRL+V Have typically proven very reliable as far as interactively determining the version of KSH you're using, however scripting them has proven more difficult.


2

The problem is that bash does integer arithmetic, so if you take, say 20/50 that is always 0. So your test which does count > 1 divided by number of lines is 0, then 0 * 100 is 0, which will always be less than 10. If you were to do the multiply by 100 before the division I think you'll get what you want.


3

Answering your question literally, here's one way to list the last PID displayed by lsof: lsof … | awk 'END {print $2}' Awk is a text processing language which reads input and processes it line by line. In the code, END {…} executes the code in the braces after the whole input is processed, and effectively operates on the last line. $2 is the second ...


2

I think that .sh.version has existed ever since the first version of ATT ksh 93. It isn't available in pdksh or mksh. Since ${.sh.version} is a syntax error in shells other than ksh93, wrap the test for it in a subshell. _sh_version=$(echo "${.sh.version}") 2>/dev/null case $_sh_version in '') echo "This isn't ATT ksh93";; … esac KSH_VERSION ...


0

Ok, I think I figured out the issue. I think the issue is the result of line 157 where I'm redirecting the output to a file; that file happens to be a mounted USB drive. I'm guessing the redirecting is done concurrently, and when product-manifest.sh is complete the script continues, eventually leading to umounting the USB drive before the concurrent write to ...


2

For "real" ksh releases (i.e. AT&T based), I use that command: strings /bin/ksh | grep Version | tail -2 Here are various output I get: Original ksh: @(#)Version M-11/16/88i dtksh; @(#)Version 12/28/93 Version not defined Modern ksh93: @(#)$Id: Version AJM 93u+ 2012-08-01 $ For pdksh/msh ksh clones and modern AT&T ksh versions too, here ...


3

KSH_VERSION was not implemented in ksh93 before version 93t. It will be set in mksh, pdksh, lksh. So for checking the version of ksh, we can try these steps: Checking KSH_VERSION to detect mksh, pdksh, lksh If first step fails, try a feature that's different between ksh93 and ksh88/86 (Let David Korn show us). With these in mind, I will go with: case ...


0

The best foolproof solution available in POSIX is the comparison of the files' device IDs provided by the stat(2) function. Perl has a similar stat function as Gilles pointed out: perl -e 'exit((stat($ARGV[0]))[0] != (stat($ARGV[1]))[0])' -- file1 file2 but the "POSIX way" is to use a C program like: ./checksamedev file1 file2 which source code is ...


0

A variation with seq, xargs, dd and shuf: seq -w 1 10 | xargs -n1 -I% sh -c 'dd if=/dev/urandom of=file.% bs=$(shuf -i1-10 -n1) count=1024'


1

You can do something like this. #!/bin/bash filecount=0 while [ $filecount -lt 10000 ] do filesize=$RANDOM filesize=$(($filesize+1024)) base64 /dev/urandom | head -c "$filesize" > /tmp/file${filecount}.$RANDOM ((filecount++)) done


1

Since you don't have any other requirements, something like this should work: #! /bin/bash for n in {1..1000}; do dd if=/dev/urandom of=file$( printf %03d "$n" ).bin bs=1 count=$(( RANDOM + 1024 )) done (this needs bash for {1..1000}).


4

Maybe something like: awk '!NF || /%/ {printf "%s", (NR > 1 ? "\n" : "") $0; sep = "\n"; next} {printf "%s", sep $0; sep = " "} END {if (NR) print ""}' < file.in > file.out


2

Try this: perl -lpe 'BEGIN{ $/= $\ = "\n\n" } s/^[^%]+?\K\n/ /gmo' <file_old.tex >file_new.tex To also leave alone lines staring with commands: perl -lpe 'BEGIN{ $/= $\ = "\n\n" } s/^\s*[^\s\\%][^%]*?\K\n/ /gmo' <file_old.tex >file_new.tex Please note however that some of the people reviewing your papers will hate you with a passion for ...


0

You're not clear on whether you want just the first : separated value or the whole remainder of the line after the "id" tag. The former is: sed -nre 's/^"id":"([^:]*):.*/\1/p' <file> The latter is: sed -nre 's/^"id":(.*)/\1/p' <file> Note the switches are important. -n ensures that nothing is printed (with the p tail printing those lines ...


0

sed 's/"id":"\([^"]*\)"/\1/g' Extracts the value if you pipe it in via stdin


1

You can very simply add more variables for the day filter: awk -v start_day=24 -v stop_day=25 -v start_time=10:00:00.000000 -v stop_time=24:00:00.999999 'start_day <= $2 && $2 <= stop_day && start_time <= $3 && $3 <= stop_time' file yields files/file1:Apr 24 10:47:00.663137 somedata files/file1:Apr 25 ...


1

Because the shell variables inside a single-quoted string, they will not be expanded. It doesn't matter that the single-quoted string includes double-quotes inside of it, it is still a single-quoted string. Use instead: grep -a volume somefile | awk "/^$time1/,/^$time2/ {print}" Actually, since print is the default action, one can simplify that to: ...


1

okay - figured it out. Posting back just incase if someone runs into it someday at sometime. The % sign has a special meaning in crontab. it's changed to newline and any string after the first % will be sent to the command as standard input. To force cron to interpret it literally, you have to escape it: 00 18 * * * rsync -a -v --delete -e ssh ...


0

I have think that you can use scp command You should create a ssh key. How to generate a key Now, you can copy the files in other server. scp file user@server.com:/path/ I hope that it work


0

Try the vim-way: ex -s +"g/MYAPP_HOME/d" -cwq file.txt


2

By design ssh doesn't allow 'embedding' of passwords - that's because it has a mechanism for non-interactive auth using public-private key pairs. So I would suggest you consider that as your first port of call. Usually it's as simple as: run ssh-keygen on your client. add the id_rsa.pub to ~/.ssh/authorized_keys on your server. If that's not an ...


6

Try: cat /sys/class/net/eth0/speed I'm not sure what you mean by primary interface. On a host with an IPv4 stack, you could retrieve the interface where the first default route is with: ip route show 0/0 | grep -Pom1 'dev +\K[^ ]+' (assuming GNU grep). So: cat "/sys/class/net/$(ip route show 0/0 | grep -Pom1 'dev +\K[^ ]+')/speed" Not all IPv4 ...


1

When running a command during boot, we don't necessarily have access to the env for that command. I needed to use the fully qualified name for that command. Doesn't work on boot mailcatcher --http-ip 192.168.50.10 Works on boot /usr/bin/mailcatcher --http-ip 192.168.50.10 My script is now functional but I do plan to add service controls so that start ...


0

I think you're seaching in a wrong way. I'd write such a script like this: if [ "z$STEP5" != "zOK" ] ; then expdp -your_options > $logfile & while ps ax | grep [e]xpdp ; do echo "expdp still in progress..." sleep 5 done wait # in order to be sure there is no more background processes... tail -n1 $logfile | grep ...


1

I keep all third-party sources in /usr/local/src, whether they come from a tarball or some third-party SCM host. (Why there? Because the default install prefix is /usr/local for most packages, so why not keep the sources alongside the binaries?) Thus, to set this up a new development machine, I simply say $ cd /usr/local/src $ sudo chown $USER . $ scp -r ...


1

myrepos could be appropriate here; it allows you to manage multiple source code repositories of different types. You can describe all your repositories in a single configuration file, and manage them simultaneously or separately as appropriate. I don't think a centralised configuration management tool such as Chef or Ansible would be a good fit here...


3

All arguments passed to shell script was stored in $@, you can loop through them: #!/bin/bash echo "Example myscript" function1() { echo "I am function number 1" } function2() { echo "I am function number 2" } if [ $# -eq 0 ]; then echo "Specify a function. E.g. function1" exit 1; fi for func do [ "$(type -t -- "$func")" = function ...


0

In your script, these two lines close to the top should do the trick: LD_LIBRARY_PATH="$(pwd)/lib" export LD_LIBRARY_PATH Although bash allows you to set and export a variable in a single statement, not all shells do, so the two step approach is more portable, if that's a concern. If this isn't working for you, check that you are running the script from ...


0

you should execute you program in this way: LD_LIBRARY_PATH=$(pwd)/lib/ <your_executable_here>


0

I think it can helps you : #!/bin/bash declare arr=('bigdeal777', 'Goog1e_analist_certs.*', 'tevq\(ucyq\)', 'GR_HOST_ID.*', "\['cmd'\]", 'ejppy.*', 'eval\(gzinflate.*', 'eval\(base64_decode*.', 'FilesMan*.', 'Web Shell by.*', 'Goog1e_analist_up.*', 'palcastle.*', 'shell_exec', 'google_analytics_obh.*', ...


1

If you can put those in a file you can use grep's -f flag to read the patterns from a file and you can use -l to show just the files that have a match Putting those together you can do something like grep -R -l -f scanner.txt * So the -R will cause it to search recursively (I'm assuming you want that), -l will print just the names of the files that ...



Top 50 recent answers are included