Tag Info

New answers tagged

0

It seems that you don't have netcat-openbsd installed in your machine. Try: zypper search netcat-openbsd Then: sudo zypper install netcat-openbsd Maybe your package name will be different, so change it to what ever zypper search command return. This will install netcat version implemented by OpenBSD. Note zypper nc-openbsd netcat implemention


1

echo -e "Hello \nWorld \n" >> greetings.txt


1

the parameter -ne is only valid for numbers, you have to use != for string comparism. This works: if [ $# -ne 2 ] ; then if [ "$1" != "--help" ]; then echo "ERROR: wrong number of parameters" echo fi echo "Syntax: $0 foo bar exit 1 fi


0

You probably want: for i in n5 n25 do if [ ${#i} -eq 2 ]; then python two.py n5 elif [ ${#i} -eq 3 ]; then python three.py n25 fi done Note that: for goes with do ... done. if goes with then ... [elif; then] ... [else; then] ... fi. the integer comparisons need -eq (equal) instead of = (for strings) and are written within ...


0

This $() is used for executing a command mostly inside some other command. chmod 777 $(pwd) pwd command gives the current working directory. So, when the whole thing is executed output of pwd will replace its position and serve as the argument to chmod , and the result is that all your present working directory get the permission 777 which I guess should ...


1

It's very similar to the backticks ``. It's called command substitution (posix specification) and it invokes a subshell. The command in the braces of $() or beween the backticks (``) is executed in a subshell and the output is then placed in the original command. Unlike the backticks, the $(...) form can be nested. So you can use command substitution ...


2

In POSIX or POSIX-like shells (ksh, bash, ash, zsh, yash...), it is like ``: the command inside $() is executed and replaced by its standard output. Word-splitting and filename generation are done unless $() is inside double-quotes. Thus chmod 777 $(pwd) should be replaced with: chmod 777 "$(pwd)" to avoid word-splitting and filename generation on the ...


1

It won't source that file by default. You can direct it to do so: ENV=~/.shrc sh Or you might add: alias sh="PS1='\$0\$ ' sh" To your usual shell's environment file. It could simplify things. Or even: alias sh='ENV=~/.shrc sh' Still, if you only want to declare the one variable for it, explicitly setting $PS1 on the command line like in the first ...


4

Just loop through the fields and check if they match or not: awk 'BEGIN{FS=OFS="|"} {for (i=1; i<=NF; i++) if ($i ~ "everyone") $i="this is what i want to see" print}' file See output: $ awk 'BEGIN{FS=OFS="|"} {for (i=1; i<=NF; i++) if ($i ~ "everyone") $i="this is what i want to see"; print}' file 123|abc|heloo good morning ...


1

sed 's/[^|]*everyone[^|]*/this is what I want to see/g' <<\DATA 123|abc|heloo good morning friends|1|123|abc|123|abc 123|abc|heloo good morning everyone|1|123|abc|123|abc DATA OUTPUT 123|abc|heloo good morning friends|1|123|abc|123|abc 123|abc|this is what I want to see|1|123|abc|123|abc This matches any occurence of ...


2

POSIXly: case $var in (*[![:blank:]]*) echo '$var contains non blank';; (*) echo '$var contains only blanks or is empty or unset' esac To differentiate between blank, non-blank, empty, unset: case ${var+x$var} in (x) echo empty;; ("") echo unset;; (x*[![:blank:]]*) echo non-blank;; (*) echo blank esac


0

I'm not sure the output of sshfs is caugh in expect. This may be your problem - the script terminates without sending the password, which would cause the problem you describe. Another possible scenario is that sshfs is actually sending you the output user@domain password:, and expect may not like it. However, this really isn't a good way of dealing with ...


3

Since you do not specify which shell, I'll just cheat. You can find the following and much else on grml.org's zsh-lover's command reference man page. $ zmodload zsh/zpty $ zpty PW passwd $1 $ zpty PW passwd $1 # ``-r'': read the output of the command name. # ``z'' : Parameter $ zpty -r PW z '*password:' # send the to command name the given strings ...


1

Regex aren't involved here. Wildcards in bash (like most other shells) only match files based on the file names, not based on the file type or other characteristics. There is one way to match by type: adding / at the end of the pattern makes it only match directories or symbolic links to directories. This way, you can move directories, then move what's left, ...


1

When you write: alias thm="cd $SET_DIR/sites/all/themes/" you're expanding the value of SET_DIR at the time you define the alias. That means you get the same value every time you run the alias, even if you've changed the variable value in between. If you backslash-escape the $ then the variable will be dereferenced when you use the alias instead: $ ...


3

You could use something like find . -maxdepth 1 \( ! -type d \) -exec sh -c 'mv "$@" MYDIR' _ {} \; First we use find to look only within the current directoy, then we ignore directories by using ! -type d finally we execute sh and move everything to the destination dir. You might try {} + at the end which will be faster.


1

I expected the qa, qa2 etc to be dummy values for more complex tests. If they are just actual values, the solution of @Gnouc is much better obviously. I'll leave this answer here for educational value related to the original examples syntax. You would want an "and" for use with the "!=". Then, you should not chain many expressions or operators together, ...


3

You should not use multiple if condition in this case, use case instead: case "$ENVT" in (qa|qa2|stageqa|stage) ;; (*) usage ;; esac


0

Here's a possible sed solution: sed '/17,9:10\/80/,/11:1/ { s/8013765024/+118013765024/ s/11:1/11:2/ }' file.txt This starts the substitution after it finds 17,9:10/80 and first appends +11 to 8013765024 making it +118013765024 and it also replaces 11:1 with 11:2 for only the first occurrence of 11:1 (the range is inclusive) ...


0

If this is an assignment, look at the other answers. If you just want to get the task done, use the calendar command, which is included in most Unix systems. On Linux: $ mkdir ~/.calendar $ echo -e "Jul 30\tMother's Birthday" >> ~/.calendar/calendar $ echo -e "08/02\tPrasant's Birthday" >> ~/.calendar/calendar $ echo -e "Aug 15\tPrabhat's ...


1

As requested, below is a working bash script. I use "=" sign field delimiter within each user 'record', and a space is the record delimiter. Note, I added fictitious emails for completeness. #!/bin/bash # DATE=$(date '+%m-%d-%Y') bdays='08-02-2014=Prashant=abc@example.com 08-15-2014=prabhat=123@example.com 09-16-2014=Aks=xyz@example.com ...


0

For those who may stumble upon a similar problem: the zip file had been processed by Maven, which attempted to use filtering. As per the Maven documentation, this filtering can corrupt binary files. After excluding zip files from Maven's filtering, unzipping worked fine.


0

Here's the answer in dodgy pseudocode. You can implement in any language you like. With a file of consistent formatted dates (called 'birthdays'). today=GET_TODAYS_DATE() While not end of file birthdays do read from file DATE NAME if today=DATE then mail address with "Today is NAME's birthday" endif endwhile Run it every day in cron at 1 ...


1

I have extended your script, so that you can run it once on startup and it will do it's job between 9PM and 9AM. #!/bin/bash -· LOGFILE="/tmp/autotest_run_count.txt" trap "echo manual abort; exit 1" 1 2 3 15 RUNS=0 while [ 1 ] ; do· HOUR="$(date +'%H')" if [ $HOUR -ge 21 -a $HOUR -lt 9 ] ; then # run program libreoffice || exit 1 ...


0

Just remove them all and then put them back: sed 's/"//g;s/,/,"/3;s/,/",/4'


-1

For testing if variable is empty or contain spaces, you can also use this code: ${name:?variable is empty}


2

Depending on what you want exactly to do, and the structure of your files, other possibilites may be available, such as : ls -1tq /dir/*/readme.txt | head -n 1 Returns the full name of the most recent readme.txt found in any subdirectory (not recursive) of /dir/ The usage of * as part of the path (not just as part of a filename) often being underknown, I ...


6

Use quotes : echo "$output" > $fn the echo line is parsed by the shell (after variable expansion), and all spaces chars between parameters are reduced to a simple space. As example : $ var1="toto > titi > tutu > " $ echo $var1 toto titi tutu $ echo "$var1" toto titi tutu


3

This can be a way: awk 'BEGIN{FS=OFS=","} # set input and output field separator as comma {for (i=5; i<=NF; i++) { # loop from 5th field gsub("\"","", $i); # remove " gsub(/^[ \t]+/,"", $i); # remove leading spaces gsub(/[ \t]+$/,"",$i)} # remove trailing spaces }1' file Removing ...


1

For more portability, you can use perl: $ perl -MList::Util=reduce -le ' BEGIN { $dir = "." } opendir DIR,$dir; print shift @{(reduce {$a->[1] > $b->[1] ? $a : $b} map {[$_,(stat($_))[9]]} grep { -d and !/^..?$/ } readdir DIR)} ' .Skype Change $dir to whatever directory you want ...


3

You don't need any elaborate pipelines for this. Moreover, you don't need to find anything - you already know where each file is, you just don't know which of them is newest. This is easily handled. To demo, here's my little test: mkdir ./dir1 ./dir2 ./dir3 for d in 1 2 3 do touch ./dir$d/samefile sleep 1 done That creates the test set. Now, which ...


2

The [[:alnum:]] character class represents alphabetic and numeric characters, you can use [^[:alnum:]] for non alpha numeric so for your goal: my target is to work with all kind of characters non alpha numeric and alpha numeric you can use this expression [[:alnum:]] | [^[:alnum:]] so the awk command will be something like this: awk 'gsub(/("[ ...


0

Short answer: look into man find and the 'time' options. This will locate the file(s), then use the -printf option to display the path of the file(s), which then can be used for cd <path> One way to use find and extract DIR of the last accessed file (within the last 24h) in current dir and its sub directories lastAccDir="$(find 2>/dev/null . -type ...


2

You probably do not need the temporary files, since you read from STDIN. So there is really no reason to use split. Get rid of the files by using --pipe: cat words | parallel --pipe -L 1000 -N1 ./script.sh If it is really just a grep you want: find dir-with-5000-files -type f | parallel -X grep -f words.txt If words.txt is too big to fit in memory, ...


3

You can use the split tool: split -l 1000 words.txt words- will split your words.txt file into files with no more than 1000 lines each named words-aa words-ab words-ac ... words-ba words-bb ... If you omit the prefix (words- in the above example), split uses x as the default prefix. For using the generated files with parallel you can make use of a ...


1

Since I don't rely on the connection or on the service, I use the following code, which tries to get the IP using different services (feel free to add more): # Get my ip address and put in a file declare -a arr=("ipecho.net/plain" "ident.me" "tnx.nl/ip" "ip.appspot.com" "https://shtuff.it/myip/short/") IP=$(curl -s --retry 3 --retry-delay 10 ...


2

The easy way to check that a string only contains characters in an authorized set is to test for the presence of unauthorized characters. Thus, instead of testing whether the string only contains spaces, test whether the string contains some character other than space. In bash, ksh or zsh: if [[ $param = *[!\ ]* ]]; then echo "\$param contains characters ...


4

If you're using GNU mv you have the following option. $ mv -b source/* dest/. This switch tells mv to push any files that collide in the dest/. directory to a backed up version, typically adding a tilde (~) to the end of the file, prior to moving files into the directory. Example Say I have the following sample directories with files. $ mkdir source ...


1

Assuming you use Bash, put the same thing in both ~/.bashrc and ~/.bash_profile. That's what I had to do, at least. Assuming you're using fink, and by all indications of how you've set everything up, it looks like you do.


0

Here is the script. If you call it move, you will have to use the command move file destination #!/bin/bash FILE=$1 DEST=$2 if `ls ${DEST}/${FILE} > /dev/null` then base=`echo ${FILE} | sed 's/\(.*\)\..*/\1/'` ext=`echo ${FILE} | sed 's/.*\.\(.*\)/\1'` r=$RANDOM mv ${FILE} ${DEST}/${base}-${r}.${ext} else mv ${FILE} ${DEST} fi ...


0

May consider mv with the interactive option -i.


0

You can use -exec option: find -regex '.*.(com\|org\|net\|biz\|info)' -mmin -1 \ -exec tail -n 1000 "{}" >> logs.txt + Now all last 1000 lines of each files in domlogs is written to file logs.txt, separated by filename. -exec command {} + tells find to run command with files found, the command line is built by appending each filename at the ...


1

In order to determine if a variable is empty you first determine if it is set. An unset variable and an empty variable are not one and the same. This can be especially useful information before trying to set a variable that has been set as empty (sometimes used to represent a condition is true for environment variables) and readonly. If you attempt to do ...


1

There are some basic bash shortcuts you should know... Ctrl + A Go to the beginning of the line you are currently typing on Ctrl + E Go to the end of the line you are currently typing on Ctrl + L Clears the Screen. Ctrl + U Clears the line before the cursor position. If you are at the end of the line, clears the entire line. Ctrl + H Same as ...


5

You can do: sudo !! Another good one is alt ., to insert the last parameter of the previous command


1

The only remaining reason to write a shell script, instead of a script in a good scripting language, is if extreme portability is an overriding concern. The legacy /bin/sh is the only thing you can be certain you have, but Perl for instance is more likely to be available cross-platform than Bash. Therefore, never write shell scripts that use features that ...


13

First, note that the -z test is explicitly for: the length of string is zero That is, a string containing only spaces should not be true under -z, because it has a non-zero length. What you want is to remove the spaces from the variable using the pattern replacement parameter expansion: [[ -z "${param// }" ] This expands the param variable and ...


3

It can be omitted if it is the last character of the event line. First, we check what ^string1^string2^ meaning from man bash: ^string1^string2^ Quick substitution. Repeat the previous command, replacing string1 with string2. Equivalent to ``!!:s/string1/string2/'' (see Modifiers below). So ...


0

I made a stupid mistake. We had few machines provisioned identically, but one (with this problem) was changed by one of my colleagues. The bad_dir was mounted NFS, which explains all problems. Thanks to Joel Davis which suggest me to check this again I solved my issue.



Top 50 recent answers are included