Tag Info

New answers tagged

0

Here's more C++ code if you want to do this from C++... #include <boost/filesystem.hpp> #include <sys/stat.h> /// returns true if the path is a mount point bool Stat::IsMount(const std::string& path) { if (path == "") return false; if (path == "/") return true; boost::filesystem::path path2(path); auto ...


2

zsh -o SOURCE_TRACE and some more words to get to at least 30 characters.


0

function isBlank { valueNoSpaces=$(echo "$@" | tr -d ' ') if [ "$valueNoSpaces" == null ] || [ -z "$valueNoSpaces" ] then echo true ; else echo "" ; fi } #Test if [ $(isBlank " ") ] then echo "isBlank \" \" : it's blank" else echo " isBlank \" \": it is not blank" fi if [ $(isBlank "abc") ] then echo ...


4

I'm not sure if I understand your question properly, but I believe you want to align the columns. If that is the case then the command column is handy: column -s, -t file Output: Thu Aug 27 2015 7:56:29 AM PoolName Total GB Used GB Available GB Percent Full Oracle-RAID10 6432.539 6179.295 253.244 ...


0

Change all occurrences of , to {tab}, except on the first line: sed '2,$s/ *, */\t/g' PoolReport.txt OR format the fields with tab separators, except on the first line: awk -F, 'NR==1;NR>1{OFS="\t"; print $1,$2,$3,$4,$5}' PoolReport.txt Output for either instance: Thu, Aug 27, 2015 7:56:29 AM PoolName Total GB Used GB Available GB ...


1

In bash you can also write that loop with explicit indexing: for ((i=2; i<=$#; ++i)); do process "${!i}" done This iterates over all arguments from the second one to the last one. If you want to exclude the last argument instead, simply make this for ((i=1; i<=$#-1; ++i)); do process "${!i}" done and if you only want to take every other ...


1

With bash: for f in XYZ*/*; do mv -v "$f" "${f%/*}/${f:0:5}${f##*/}"; done The for loop runs trough all XYZ* directories. Then the mv command renames the files. Where: $f is the original filename ${f%/*} is the directory name ${f:0:5} is the prefix ${f##*/} is the original filename


0

You can run this command which just shows you what it would do: ls -d XYZ*/* | sed -n s'|\(XYZ[0-9][0-9]\)\([^/]*\)/\(\2_.*\)|mv & \1\2/\1\3|p' | cat and if you like the commands to run, replace cat by sh.


1

With zsh: autoload zmv # best in ~/.zshrc zmv '(XYZ??)(*)/(*)' '$1$2/$1$3'


2

When the shell parses a command line, it removes quotes but remembers the text structure that they imply.  (That is a gross oversimplification; see bash(1) or Shell Command Language for more details.)  For example, the command-line input -blah apple -secondfruit "green banana" -some more causes the shell to identify six words, or tokens: -blah apple ...


2

If it must be with echo "Kate", use awk: echo "Kate" | awk 'NR==FNR{a=$0;next} sub("Steven", a, $0)1' - file The standard input is Kate from echo "Kate", piped to awk. awk then reads the stdin (-). The condition NR==FNR is true when the first file is processes (stdin). The variable a is set to that value. Then the input of file is processed and sub() ...


2

There's always the caveman approach: first=1 for i do if [ "$first" ] then first= continue fi something with "$i" done This leaves $@ intact (in case you want to use it later), and simply loops over every argument, but doesn't process the first one.


1

When you refer to a variable without quotes around it (e.g. echo $files), the shell splits the value apart on whitespace and passes each term as a separate command-line option. Newlines are treated the same as any other whitespace. The echo program doesn't see the newline characters at all; it just gets an array of strings, each of which is a single ...


1

When referencing a variable, it is generally advisable to enclose its name in double quotes. This prevents reinterpretation of all special characters within the quoted string -- except $, ` (backquote), and \ (escape). Keeping $ as a special character within double quotes permits referencing a quoted variable ("$variable"), that is, replacing the variable ...


2

You have newlines because ls puts them on separate lines. The newlines disappear without the quotes because the shell (bash) passes each unquoted space separated text to the command as a separate argument. Note: The command substitution is done by the shell, not by ls, so you do not need ls. Therefore you can do #!/bin/bash echo *.fastq or ...


17

First, note that $@ without quotes makes no sense and should not be used. $@ should only be used quoted ("$@") and in list contexts. for i in "$@" qualifies as a list context, but here, to loop over the positional parameters, the canonical, most portable and simpler form is: for i do something with "$i" done Now, to loop over the elements starting from ...


6

I think you want the shift builtin. It renames $2 to $1, $3 to $2, etc. Like this: shift for i in "$@"; do echo $i done


0

Here's the solution you've been looking for: function xtrace() { # Print the line as if xtrace was turned on, using perl to filter out # the extra colon character and the following "set +x" line. ( set -x # Colon is a no-op in bash, so nothing will execute. : "$@" set +x ) 2>&1 | perl -ne 's/^[+] :/+/ and print' 1>&2 ...


1

test -f won't work for multiple files expanded from wildcards. Instead you might well use a shell function with null-redirected ls. present() { ls "$@" >/dev/null 2>&1 } if [ $# -lt 1 ]; then echo "Please enter the path" exit fi path=$1 if ! present $path/cc*.csv && ! present $path/cc*.rpt && ! present ...


-1

It should work if you remove the outer square brackets in the second if condition


0

I'd suggest to write some script/small program that employs XML parser. Then you can count records as they getting parsed and filter out only the stuff you need.


1

Assuming a textbook example shell (for code clarity) that is already running (so the dynamic linker is done), the commands you mention will require the shell to make the following system calls: read: gets the next command in this case gcc fork: two process are needed, we assume the parent has pid 500 and the child for illustration. the parent will call ...


1

If I understand the question correctly, you want to rsync all the files in the range A0000000 through A0000095.  Well, then, don’t say A*; use a list of positive wildcards (a.k.a. globs or filename expansion patterns) that generate the file names you want, rather than identifying the ones you want to exclude.  Do it by decomposing the range into subranges: ...


1

You were almost there with your first attempt. The problem is that adb unhelpfully adds a carriage return at the end of every line. You can't see it in the basic usage where the output is printed to the terminal, because a carriage return at the end of a line has no visual effect (the carriage return moves the cursor to the beginning of the current line, but ...


0

Since the numbers are padded with zeros to the same width, the numerical order is identical to the lexicographic order. Therefore your problem is equivalent to removing the files starting with a given file in the lexical order. You can do this by building a string containing the file names separated by newlines, and using string substitution to remove the ...


3

Well, the exact sequence may vary, as there might be a shell alias or function that first gets expanded/interpreted before the actual program gets executed, and then differences for a qualified filename (/usr/libexec/foo) versus something that will be looked for through all the directories of the PATH environment variable (just foo). Also, the details of the ...


1

As you mentioned that it should be an automated shell script, at least for the two yum commands you would need to add -y so yum will assume an answer of "yes" for all questions it will ask. See the yum man page, relevant excerpt: -y, --assumeyes Assume yes; assume that the answer to any question which would be asked is yes. ...


0

First what comes to my mind is to get PID after process creation, use zstat tool (see man zshmodules) to get get process timestamp, e.g. zstat '+mtime' /proc/PID, and after desired amount of time check if timestamp for given /proc/PID has no changed - if yes kill the job.


4

Many tools can be handy: -n of grep is exactly what you are looking for. grep -n 'bla' file alternatively awk: awk '/bla/{print NR":"$0}' file alternatively perl: perl -ne 'print $.,":",$_ if /bla/' file alternatively sed: sed '/bla/!d;=' file |sed 'N;s/\n/:/'


0

A modern cat implementation (sunos-4.0 1988) uses mmap() to map the whole file and then calls 1x write() for this space. Such an implementation will not loop as long as the virtual memory allows to map the whole file. For other implementations it depends on whether the file is larger than the I/O buffer.


0

mapfile -t parameters < <(sed '1,4s/.*=//' abc) parameters[2]=$(printf '%s\n' "${parameters[@]:2}")


0

As written in Bash pitfalls, you cannot read from a file and write to it in the same pipeline. Depending on what your pipeline does, the file may be clobbered (to 0 bytes, or possibly to a number of bytes equal to the size of your operating system's pipeline buffer), or it may grow until it fills the available disk space, or reaches your operating ...


0

The problem is that with while read you are reading the file line by line. As the certificate spreads among multiple lines (with explicit newline characters), you cannot simply read it in as you do. If all files are like this and do not have multiple certificates or any other content, you can do this: Delete lines 1 and 2, remove certificate= and read rest ...


0

I recommend to use star as star comes with a builtin find. This enables features that cannot be achieved with a separate find command.


1

Since /etc/rc.local is executed at the end of each multiuser runlevel, it's not the correct place to add start scripts. I recommend to not use /etc/rc.local in any way. It's a reclit for early *nix times. Instead of that, create a startup script in /etc/init.d/name which accepts start and stop arguments to start or stop the deamon, process or the job: #! ...


3

I could reproduce the phenomenon on Ubuntu 15.04 with the following crontab: * * * * * { echo job 0; } & sleep 5 * * * * * { echo job 1; } & * * * * * { sleep 5; echo job 2; } & I got mails from cron with job 0 every minute, mails with job 1 occasionally (5-6 times in last 10 minutes), no mails with job 2. So it seems cron waits for the child ...


3

Upon further testing, I suspect the & is messing with your results. As you point out, &>/dev/null is bash syntax, not sh syntax. As a result, sh is creating a subshell and backgrounding it. Sure, the subshell's echo creates stderr, but my theory is that: cron is not catching the subshell's stderr, and the backgrounding of the subshell always ...


0

This is how you would create a dynamically named variable (bash version < 4.3). # Dynamically named array my_variable_name="dyn_arr_names" eval $my_variable_name=\(\) # Adding by index to the array eg. dyn_arr_names[0]="bob" eval $my_variable_name[0]="bob" # Adding by pushing onto the array eg. dyn_arr_names+=(robert) eval $my_variable_name+=\(robert\) ...


3

Your variable syntax is wrong. A variable is quoted as ${n}. Hence, try sed -i -e "1,${n}d" filename


1

You are unlikely to find such a tool, as csh is widely viewed to be inappropriate for shell scripting. If you have any Bourne-derived shells available to you (e.g. sh, ksh, mksh, ash, dash, bash), you should consider reimplementing your work in one of those, or use a more complete scripting language such as Awk or Perl. You'll find far more robust tools for ...


0

The dig(1) or host(1) commands can be automated to lookup A records: % for d in example.com example.org; do dig +short A $d; done 93.184.216.34 93.184.216.34 % for d in example.com example.org; do host -t A $d; done example.com has address 93.184.216.34 example.org has address 93.184.216.34 % As a script, one way would be: $ cat domlu ...


1

You can use the shuf command to get a random number between a a range Numbers. Ex: $ shuf -i 70-100 -n 1 79 $ shuf -i 70-100 -n 1 72 $ shuf -i 70-100 -n 1 95 $ shuf -i 70-100 -n 1 96 $ shuf -i 70-100 -n 1 81 $ shuf -i 70-100 -n 1 78 $ shuf -i 70-100 -n 1 94 $ shuf -i 70-100 -n 1 78 $ shuf -i 70-100 -n 1 89 $ shuf -i 70-100 -n 1 81 $ shuf -i 70-100 -n 1 97 ...


1

You need to run dos2unix on your file, which is presumably copied into your Linux distribution with Windows file endings. The ^M, aka the carriage return character, trips your Linux distribution into thinking it needs to look for a command bash^M, instead of bash. To actually view the ^M characters, you can open your file with cat -A: $ cat -A file # --- ...


0

cdmage loads in multi track files and saves them to 1 cue+bin file. Just tested it on psx Lucky Luke with 21 tracks and game runs smooth on my emulator after.


0

This is portable: a="$(command)" # Get the output of the command. b="????" # as many ? as characters are needed. echo ${a%"${a#${b}}"} # select that many chars from $a To build a string of variable length of characters has its own question here.


3

The utility that's actually designed for this is csplit NAME csplit - split a file into sections determined by context lines SYNOPSIS csplit [OPTION]... FILE PATTERN... So in this case csplit file /Fub_Level_DACGE\(%\)/ will produce files xx00 and xx01 containing the two parts.


1

This will read file file0 and write its lines to file1 until the line starting Fub_Level_DACGE is reached. Starting with that line, all lines are written to file2: awk -v f=file1 '/^Fub_Level_DACGE/{f="file2"} {print>f}' file0 How it works -v f=file1 Set the variable f to the name of the initial output file, file1. /^Fub_Level_DACGE/{f="file2"} ...


0

Thanks for all the help, although I haven't been able to get some of these to work. I did, however, successfully implement code I found in an answer to another question! (http://unix.stackexchange.com/a/87476) export DIR=./folder if ls ${DIR}/*.RAW &>/dev/null then echo "Yes!" else echo "No!" fi


-1

Two Bourne Shell Solutions With the Bourne shell (which doesn't support arrays, or shell options like nullglob or failglob) you have to work around the fact that a non-zero exit status or the glob itself will be returned if a glob isn't found. For example: if [ -n "`ls *.RAW 2> /dev/null`" ]; then echo "At least one RAW file found!" else echo ...


0

Your example answer is almost correct. First, you need to separate the testing string ".RAW" from the closing ]: if [ ! -f "*.RAW" ]; You can do shell globbing in this context by taking the asterisk outside of the quotes. So the finished command would be: if [ ! -f *".RAW" ]; However, this approach isn't so useful if you want to use the .RAW files. ...



Top 50 recent answers are included