Tag Info

Hot answers tagged

24

Since you use brace expansion anyway so make use of its feature fully: echo {375..3500..5} To print each number in separate line with optional text: printf "Number %s is generated.\n" {375..3500..5} Number 375 is generated. Number 380 is generated. Number 385 is generated. ... Edit As pointed out by @kojiro in the comment Mac OS uses bash 3 as the ...


18

Alternatively a traditional C-style for loop can be used: for ((i=375; i<=3500; i+=5)); do echo $i done This is perhaps less clear than using seq, but it doesn't spawn any subprocesses. Though since I'm familiar with C, I wouldn't have any difficulty understanding this, but YMMV.


15

Using SEQ(1) for i in $(seq 375 5 3500) do echo $i done Or, simply: seq 375 5 3500


11

Your for-loop snippet didn't work as you require for two reasons: (($i += 5)) - here the $i is expanded to the value of i. Thus the expansion will be something like ((375 += 5)), which doesn't make sense (attempting to assign a literal number to another literal number). This would normally be achieved with ((i += 5)) (no $ to expand the variable) The ...


9

First check if the path to add is already part of the variable: [[ ":$PATH:" != *":/path/to/add:"* ]] && PATH="/path/to/add:${PATH}" If /path/to/add is already in the $PATH, then nothing happens, else it is added at the beginning. If you need it at the end use PATH=${PATH}:/path/to/add instead. Edit: In you case it would look like this: [[ ...


6

Suppose that we have this test file: $ cat file the cat in the hat the quick brown dog jack splat With grep implementations that have adopted GNU's -o extension, we can retrieve all the words containing a: $ grep -wo '[[:alnum:]]*a[[:alnum:]]*' file cat hat jack splat We can count those words: $ grep -wo '[[:alnum:]]*a[[:alnum:]]*' file | wc -l 4


5

The sysadmin in my old lab had a nifty little function for this: pathmunge () { if ! echo "$PATH" | /bin/grep -Eq "(^|:)$1($|:)" ; then if [ "$2" = "after" ] ; then PATH="$PATH:$1" else PATH="$1:$PATH" fi fi } It will both check whether the string given is already in the PATH and ...


5

While there is, of course, an app for that (seq 375 5 3500), there are various ways of doing this from the commandline. While the fastest and simplest will be just using seq, here are some other options: for i in {375..3500}; do [[ (($i % 5)) -eq 0 ]] && echo $i; done i=370; while [ $i -le 3500 ]; do printf "%s\n" $((i+=5)); done perl -le ...


5

POSIXly: <file tr -s '[:space:]' '[\n*]' | grep -c a Here, words are sequences of non-spacing characters.


4

Don't use password authentication. Use ssh keypairs. Karthik@A $: ssh-keygen #keep the passphrase empty Karthik@A $: ssh-copy-id B #enter your B password #^ this will copy your public key to Karthik@B:.ssh/authorized_keys From then on, you should be able to ssh from A to B (and by extension, scp from A to B) without a password.


4

push(){ shuttle push note Chrome \ "Aurora: $1" \ "Battery is at $percent percent" } full=0 while percent=$(acpi | awk '{ print $4}' | sed 's/[,%]//g') do case $percent:$full in (100:1) ;; (100:0) full=1 push 'Battery charged';; (?:*|1?:*|20:*) full=0 ...


4

POSIXly: i=370 while [ 3500 -gt "$i" ] do echo "$((i+=5))" done ...or... echo 'for(x=370;x<=3500;x+=5)x' |bc I dunno why you'd do it any other way. Excepting, of course... seq 375 5 3500 ...or with dc: echo '370[5+pd3500>p]splpx'|dc


3

Yes, you can use vi commands in a stript: sed — stream editor, is a member of the vi family (ed, vi, sed). It is used to edit streams of data passed via pipes, so just what you need. It also uses same commands as vi. e.g. To process standard in and put result in Updatedfile sed -e "s/Value/$NewValue/g" > Updatedfile see also -i option, to edit ...


3

The splitting into different parts (command & arguments) does not work if variables are used. Use eval for this case: eval $_rscmd --exclude=alternatives /etc/ $_mn | eval $_grep >> "$_rslog" In general, it is better to use shell functions or aliases than using variables: alias my_grep='grep -E "^deleting|[^/]$"' ... ... | my_grep >> ...


3

there are zillion way to do it, first two I came with are awk and grep (in that order) awk awk '/debug/ { print > "debug.txt" ; next ;} { print } ' logfile > info.txt where /debug/ select line with word debug { print > "debug.txt" ; print to debug.txt next ;} read next line in logfile { print } if no debug, print to stdout > info.txt ...


3

Fedora uses RPMs. Assuming the util-linux RPM is installed on your system, the command to show the pre-removal scriptlet is: rpm -q --queryformat '%{PREUN}\n' util-linux You can check if the "util-linx" RPM is installed with this command: rpm -q util-linux If you want to show all RPM scriptlets for util-linux, you can use this command: rpm -q ...


3

For ksh93, you have (at least) a couple of choices associative arrays envir=Dev foo["$envir"]=bar echo "${foo["$envir"]}" namerefs nameref var=${envir}foo var=bar echo "$var" For ksh88, you may be stuck with eval: envir=Dev name="${envir}foo" eval "$name=bar" eval "echo \$$name"


2

Naive Solution I will assume that your logs have file names which match the glob /xx/xx/logs/xx/*.log and that you want to send mail to somebody@host.com newest=$(ls -rt /xx/xx/logs/xx/*.log | tail -n 1) echo 'An error occured' | mutt somebody@host.com -s "Logs" -a "$newest" This approach works if the files have well-behaved names. In general, though, ...


2

Here is a bash script to do the job : while IFS= read -r line; do if [[ $line =~ debug ]]; then echo "$line" >>debug.txt else echo "$line" >>info.txt fi done <log.txt


2

I think there's more to this: Either that's not the command you're using - or else somewhere else in the function you're doing it differently. That error comes from ${1?}. Or it comes from your test, but only if you first do set -u. To fix that, stop doing that. Do set +u; fn_name, and see what happens. And if you have any ${1?} expansions in there, that ...


2

The usual way is to make only command's arguments as variable _mn=/backup.raw/ _rscmdarg='-av --delete -R' _greparg='-E "^deleting|[^/]$"' _rslog=/var/log/rsync.log as $_rslog is a file, and $_mn a dir, it is okay to keep them. and the "final" line is: rsync $_rscmdarg --exclude=alternatives /etc/ $_mn | grep $_greparg >> $_rslog


2

How about: if [ "$percent" -eq 100 ] && [ "$full_flag" -eq 0 ]; then shuttle push note Chrome "Aurora: Battery charged" "Battery is at $percent percent" full_flag=1 fi if [ "$percent" -lt 100 ]; then full_flag=0 fi


2

Think about the first line of your script: for myvar. How many times will this for loop be executed if there are no arguments?


2

Since the number of files is large, awk sounds like a good choice: awk ' {line[FNR] = line[FNR] $1 OFS} END {for (i=1; i<=FNR; i++) print line[i]} ' file1 file2 file3 ...


2

If you're stuck on Bash 3: echo {375..3500} | tr ' ' '\n' | sed -n 'p;n;n;n;n' and if you prefer awk: echo {375..3500} | tr ' ' '\n' | awk '!((NR-1)%5)' I didn't know about brace expansion -- that is seriously cool.


2

That should do it: grep -rlZ "wrongtext" ~/Library/Calendars | xargs -0 sed -i '' 's/wrongtext/righttext/g' I added the -Z parameter to grep to add a zero byte instead of a newline after every filename. So the command works also with strange filenames. xargs then reads the input delimited by the zero byte with -0 and calls the sed command.


2

Nowadays, almost every login-related software relies on pam, so it may be a good idea to use pam_exec module to execute your command after the login request to pam (and thus, before the return to the application). You may also look directly at pam modules, like described in this thread.


1

If it's the same string, then just do it: set -a -- "$OPENSHIFT_HOMEDIR/app-root/runtime/bin/" PATH=$1:${PATH#"$1:"} set +a -- You can do that over and over and the value of $PATH won't change. Alternatively you can check for it. set -a -- "$OPENSHIFT_HOMEDIR/app-root/runtime/bin/" case :${PATH:=$1}: in (*:"$1":*) ;; (*) PATH=$1:$PATH esac; set +a ...


1

With GNU sed you can do it by echo ${PATH} | sed "\|${OPENSHIFT_HOMEDIR}/app-root/runtime/bin|\ ! s|^|export PATH=${OPENSHIFT_HOMEDIR}/app-root/runtime/bin:|e"


1

You can use sleep with while loop as follows: while true; do echo "1" > /sys/kernel/mm/ksm/run; sleep 120; echo "0" > /sys/kernel/mm/ksm/run; sleep 7200; done; Here, while Loop starts with condition while true; that means no condition for stopping loop (it runs forever until script is killed) then commands will be run (with sleeping as you wish) at ...



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