Tag Info

Hot answers tagged

6

Associative array in bash (and in other languages) does not preserve the order of elements in declaration. You can add another associative array to keep track the order of declaration: YELLOW=$'\e[93m' declare -A OP=( [Description]="remote to destination" [Source]="/var/www" [Destination]="/foo/bar" ...


5

As I said in my comment, it's generally not a good idea to parse HTML with Regular Expressions, but you can sometimes get away with it if the HTML you're parsing is well-behaved. In order to only get URLs that are in the href attribute of <a> elements, I find it easiest to do it in multiple stages. From your comments, it looks like you only want the ...


5

The main problem is that you loop over output of ls command. Use glob * instead: #!/bin/sh cd ~/Data for i in *; do echo "$i" filename="$i" date=$(date -n +%Y-%m-%d) new_filename="${date}${filename}" echo mv "${filename}" "${new_filename}" mv -- "${filename}" "${new_filename}" done Additionally I added -- to mv in order to treat ...


5

Your function has an exit status but no output. Your variable $a will always be empty, so the [[ $a ]] test will always be "false" You truly want this: if is_equal 42; then ... But what you think you want is this is_equal 42 # don't capture the output a=$? # but do grab the exit status if [[ $a -eq 0 ]]; then ...


5

Assuming you want to use A && B || C, then simply call the function directly: tst && echo "success" || echo "failure" If you want to use [[, you'd have to use the exit value: tst if [[ $? -eq 0 ]] then ...


4

If your grep supports Perl regexes: grep -Po '(?<=href=")[^"]*(?=")' (?<=href=") and (?=") are lookaround expressions for the href attribute. This needs the -P option. -o prints the matching text. For example: $ curl -sL https://www.google.com | grep -Po '(?<=href=")[^"]*(?=")' /search? https://www.google.co.in/imghp?hl=en&tab=wi ...


4

You can use this command: awk '{print $1}' filename > newfile where filename is the name of the original big file and newfileis the file that will get the results.


4

(this is not really an answer, more of a comment) You have to be a bit careful about the a && b || c shortcuts: if a returns success, then b is executed if b subsequently returns an exit status, then c will be executed too. $ [[ -f /etc/passwd ]] && { echo "file exists"; false; } || echo "file does not exist" file exists file does not ...


3

You should never install files into home directories with packages. Instead, you can install default configuration files to /etc/skel so that new users created after your package is installed will have these files copied to their newly created home directories. Users that already exist will not get these new files though. Your application can create config ...


3

Basic example of sed usage using this as test.txt: one two three two four two five To replace two with foo in that file: sed -i 's/foo/two/g' test.txt What that means: sed is the name of the command, you'll find lots of tutorials (e.g.) and other documentation online, in addition to man sed. -i means edit a file in place. 's/foo/two/g': the s ...


3

You seem to be doing things in a needlessly complicated way. Why not just dpkg -l curl || apt-get -y -qq install curl > /dev/null 2>&1 You did ask for a one-liner after all. Since, presumably, all you want is to know whether curl is available, you could also just do type curl >/dev/null 2>&1 || apt-get -y -qq install curl


2

I don't use dash, but here is what bash manual has to say about aliases: The rules concerning the definition and use of aliases are somewhat confusing. Bash always reads at least one complete line of input before executing any of the com‐ mands on that line. Aliases are expanded when a command is read, not when it is ...


2

If you're not averse to using eval: $ busybox ash -c 'a()(alias x=echo\ hi;type x;alias;eval x);a' x is an alias for echo hi x='echo hi' hi I have no idea why this works.


2

Grep exits by default with 0 on match and 1 on no match. As such you could do: grep -q "targed file \$1 is patched sucesfully, enjoy new kernel" foo.log The -q suppress any output. To test say something like: grep -q "targed file \$1 is patched sucesfully, enjoy new kernel" foo.log && echo OK || echo BAD If the $1 is actually replaced with a ...


2

When you type function_under_test, the shell think it's a command, not a variable. You need to expand it, so function_uneder_test will be expanded to sum_squares. Change your line 32 to: "$function_under_test" "$3"


2

{1..$num_in} is a kshism/zshism. You should write: `seq $num_in` Note: Though bash supports code like {1..3}, as said by 1_CR in comment, {1..$num_in} doesn't work in bash, due to the fact that brace expansion precedes parameter substitution. So, it probably comes from ksh93 or zsh, where it works because parameter expansion is done first.


2

There are two bugs in your code, one serious the other not so much: You are using backticks twice. Also don't use backticks. Use command substitution as the following: $(command ...) query=$(dpkg-query -W -f '${Status}') Query will already contain the result of the command call. When you now execute `$query` you will now try to execute the result, ...


2

If you're jailing the user, you should probably just give him a copy of the script. If you set him up with a chrooted account, he wouldn't see anything outside of his home directory.


2

Not sure if you are limited on tools: But regex might not be the best way to go as mentioned, but here is an example that I put together: cat urls.html | grep -Eo "(http|https)://[a-zA-Z0-9./?=_-]*" | sort | uniq grep -E : is the same as egrep grep -o : only outputs what has been grepped (http|https) : is an either / or a-z : is all lower case A-Z : is ...


2

As a non-regex alternative, use pup: pup 'a[href] attr{href}' < yourfile.html Will find all a elements that have a href attribute, then display the value of the href attribute. To install pup, you need Go (a programming language): sudo apt-get install golang sudo go get github.com/ericchiang/pup The advantage of this solution is that it doesn't ...


2

Your code is quite ok, I'd probably do that as a quick solution. It's more efficient to actually exit sed immediately after printing: sed -n '1{p;q}' to avoid reading the entire file for no reason. Now the most awkward part is reading the file twice to get first two lines. You could simply use shell builtins: { read -r var1; read -r var2; } < infile ...


1

It all depends on what you want. If you want to process files one after the other, you can simply call your awk script on both files sequentially with a loop, and redirect the output: (for file in outfile1.out outfile2.out; do awk -f awk_file.awk < "$file"; done) > text_file.txt or in this case of two files, simply (awk -f awk_file.awk < ...


1

From wget manual: ‘--user=user’ ‘--password=password’ Specify the username user and password password for both FTP and HTTP file retrieval. These parameters can be overridden using the ‘--ftp-user’ and ‘--ftp-password’ options for FTP connections and the ‘--http-user’ and ‘--http-password’ options for HTTP connections. So what you ...


1

The other problem you had was that you also had extra quotes on the mv line. When you are using echo to print a command that will be copied to a shell you need quote everything twice, once for this shell, and once for the second. your echo mv worked fine as long as the filename did not begin with a dash or contain a single quote. The mv only needed quoted ...


1

Arrays are defined differently: components=(Persistence Instrument "Accessory Engine") or components=(Persistence Instrument Accessory\ Engine) And accessed differently: for i in "${components[@]}"


1

var=$( cat foo.txt ) would store the output of the cat in variable var. var=$( ./myscript ) would store the output of myscript in the same variable.


1

If you have to cd in and out of several directories then it makes sense to use cd - instead which takes you to the last current working directory. Or you use pushd / popd (in bash). for directory in *; do pushd "$directory" index=1 for filename in *; do extension="${filename##*.}" if [ "$filename" != "$extension" ]; then ...


1

As indicated in the comment to the question man rsnapshot says: EXIT VALUES 0 All operations completed successfully 1 A fatal error occurred 2 Some warnings occurred, but the backup still finished So you can modify your command line for example: rsnapshot ... with_all_arguments ; [ $? == 0 ] && rm -f flag.file || ...


1

Because {1..$num_in} did not expanded to sequences of numbers, it's only expanded to literal string like {1..1}, {1..2} and so on. So, your script performed arithmetic expansion, it saw an invalid number, and print error message. When you use your shebang as #!/bin/sh, it depends on system to use what shell /bin/sh linked to for running your script. Thus, ...


1

grep is the way to go, it returns 0 if a match is found. You don't actually need to output the line, so just discard the line and use the test. In your case, it would just be lastline=$(tail -n1 logfile) if grep pattern <<<"$lastline" &>/dev/null; then echo "yay, found pattern" else echo "darn" fi Observe the "here string" ...



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