Tag Info

New answers tagged

0

You set the location in a variable named $PATH. This variable has a special meaning as the search path where the shell will look for commands to execute. By setting it to a directory which you created (and which therefore is empty), you just made sure that the shell can't find any command anymore. Rename that variable to something else. All will be fine ...


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"


-1

[ ${FOO:-0} == 0 ] && FOO=1 || FOO=0 or even: [ ${FOO:-false} == false ] && FOO=true || FOO=false should do the job


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

The more I read this question, the less sure I am that I understand it.  I’m going to assume that it is as follows: You have a directory tree that looks like . ├───dir1 ⋯ Modified 140 days ago. │ ├───dir11 ⋯ Modified 160 days ago. │ ├───dir12 ⋯ Modified 140 days ago. │ ├───dir13 ⋯ (Don’t care.) │ ├───dir14 ...


1

You have the commands, so put them in a script! To run a bunch of commands on different data, put the changing data in a variable. To run gcov and mv on all the files, there are several possible methods, including: Run gcov on all files, then move them. Run gcov on one file, then move its output. Run gconv on the files in a directory, then move them. ...


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.


0

So I copied your bottom thing into two files. File 1: { "section1Configs": { "level1Settings": { "section01Lev01_01": true, "section01Lev01_012": true, "section01Lev01_02": true } } } And File 2... { "section1Configs": { "level1Settings": { "section01Lev01_01": true, ...


0

May be better to use tools specialized for json like jq or jshon? But if you'd like it can be done with paste and sed paste config master | sed '/[{}]/! {/\(.\+\)\t\1/d;};s/\t.*//' or awk awk '{getline a < "master"} /[{}]/ || $0 != a' config for each line in config-file gets corresponding(by number) row from master-file into variable a and if line ...


0

Figured it out. It does not like the -O grep -rlZ "wrongtext" ~/Library/Calendars | xargs sed -i '' 's/wrongtext/righttext/g'


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.


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


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


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


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


0

Simple approach: $ cat file1 0.12 0 0.32 0 0.42 1 0.23 0 $ cat file2 0.92 0 0.92 0 0.92 1 0.93 0 $ cat file1 file2 | cut -f1 -d" " 0.12 0.32 0.42 0.23 0.92 0.92 0.92 0.93 $


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


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"


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


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


0

Which version of inotifywait are you using? According to this man page -d specifies that you want to run it as a daemon, but it doesn't take an argument value ("$dir"). Also with -o "$log" it will be saving output to the log file and not sending it to standard output. Make sure you have a working inotifywait command before continuing with the script - ...


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


0

The simple way to do it is to use single quotes '…' around the string. Single quotes delimit a literal string, so you can put it anything between them except a single quote '. To insert a single quote in the string, use the four-character sequence '\'' (single quote, backslash, single quote, single quote). Technically, there's no way to put a single quote ...


-1

Yes, you should first check if there are any arguments or not and then proceed. if [ $# -gt 0 ] then for myvar do if ! [ -e "$myvar" ] echo "$myvar cannot be found." exit fi echo "This is the filname:" $myvar cut -f1 -d, $myvar > social_security cut -f2 -d, $myvar > ...


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?


0

You could combine all files into one by feeding two commands into find: output first the file name (or a preprocessed version thereof) and then the contents: find /home/ -maxdepth 2 -iname ".bash_history" -type f \ -exec sh -c "echo {} >> combined.txt && cat {} >> combined.txt" \;


0

I ended up just writing to a file: find ~/dim_import/* -type f ! -name xdir | cut -d '/' -f 5-6 > files and then looping through the "files" file and creating the directories from that and then copying the files there. while read line; do fileDir=`echo "$line" | cut -d '/' -f 1` # get folder name fileName=`echo "$line" | cut -d '/' -f 2` # ...


1

apt-get -qy update > /dev/null apt-get -qy dist-upgrade >> /var/log/apt/scripted-upgrades.log You can send them both to /dev/null if you want-- but once its gone you can never look at what went wrong after issuing the command. Also if your /etc/apt/sources.list is in bad shape, running a plain interactive apt-get update should clue you in. ...


1

continue jumps to the next iteration of the loop. break exits the loop altogether. Neither is useful if you want to have some effect on future iterations. For that, you need to remember the information that something has to behave differently. The way to remember some information is to store it in a variable. One way to do what you want is to store the ...


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


1

The problem most likely is because your initscript start action doesn't exit. Initscripts in general should start daemon processes and exit after the process has been started (or do one-off jobs, like mounting a filesystem for example). You should propably use start-stop-daemon to "daemonize" motion. The Debian initscript template should be in ...


1

You need to start motion as a daemon, using start-stop-daemon or something similar. The simplest solution is to use the motion package in Debian and its init script, which starts motion as follows: start-stop-daemon --start --oknodo --exec $DAEMON -b --chuid motion (DAEMON is /usr/bin/motion). To have the daemon start automatically you need to edit ...


0

This script will download bar.tar file in every "public" directory in /home/*/ #!/bin/bash base="$PWD" for dir in /home/*/public/ do cd ${dir} wget http://foo/bar.tar -O - | tar x cd ${base} done Of course that can be done in many different ways, but this seems easiest to understand.


0

sed -e:n -e'/\n.*Banana/!{$!{N' \ -e'/Banana/bn' -eb -e\}\ -e's/\n/&Orange&/;t' \ -e'/Banana/a\' -eOrange\ -e\} -e'P;D' <<\IN Apple Banana Apple Banana IN Apple Banana Apple Banana Orange That should work pretty reliably. It will buffer in pattern space between occurrences of Banana in input - printing to ...


0

Try this: sed '$s/$/\nOrange/' file sed '$p;$c Orange' file sed -e '${p;c Orange' -e '}' file $ is last line and in a regex end of line.


0

tac a | sed '0,/Banana/{s/Banana/Orange\nBanana/}' | tac Will do it ! EDIT : Modified from the answer to this question - http://stackoverflow.com/questions/18291341/sed-only-the-last-match-pattern


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


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


0

Here is a less complex script than what Graeme provided. His script didn't work for me, $who_line was always empty. My script doesn't waste so much time with finding a process. Instead, it just tries all and pick the last useful value found. I'm running xubuntu 14.04 and have some lxc containers running which probably confuse this kind of scripts. env="$( ...


0

To be more clear consider cat <<EOF foo bar $baz EOF fubar="Hello World" cat <<EOF echo $fubar print \$fubar EOF when run will give foo bar echo Hello World print $fubar on first cat you'll notice that $baz variable disappear (provided it is not set). on second run, I set fubar variable, it is echoed with it's value, to echo ...


0

Since you already know how to filter the files, then use recursive copy: cp -R Or you could just use rsync with --prune-empty-dirs option. rsync --exclude='*xdir*' --prune-empty-dirs ~/dim_import ~/new Note: If you don't use trailing slash from source (like above's example) dim_import is also copied.


1

Is there true way without user ncurses interface? echo Antarctica/Mawson >/etc/timezone dpkg-reconfigure -f noninteractive tzdata Yes, you can create one shell command: sh -c 'echo Antarctica/Mawson >/etc/timezone && dpkg-reconfigure -f noninteractive tzdata' :)


0

You can try using the date command. Running date with no options will output the system date and time, as in the following output: date Thu Feb 8 16:47:32 MST 2001


0

As HP-UX does not have bash (available from third party), you can use globs instead of regex. This works with both sh and ksh on HP-UX e.g. if [[ ${Y} = @(ERROR|ORA-)* ]] ; then exit 1 fi


1

Here is a method that only uses POSIX shell features: find ~/dim_import/* -type f ! -name xdir -exec sh -c ' p=${1%/*}; d=${p##*/}; f=${1##*/}; mkdir -p new/"$d"; cp "$1" new/"$d"' -- {} \;


0

xargs should be available. You could use: find ~/dim_import/* -type f ! -name xdir | xargs -I {} cp {} new/{}


0

Any specific reason you cannot use check_swap from nagios-plugins? check_swap -w 80% -c 60% If you insist on using sar, why calculate an absolute value for the threshold at all, can't you just use the value from the %swpused column to determine check state? SWPUSED=$(sar -S -f $SA | tail -2 | head -1 | awk '{print $5}') SWPUSED_ROUNDED=$(printf "%.0f" ...



Top 50 recent answers are included