Tag Info

Hot answers tagged

20

You already have the basic idea. If you want to code this in bash (which is a reasonable choice since it is the default shell on Ubuntu and most other Linuxes), you can't use case because it doesn't understand ranges. Instead, you could use if/else: #!/usr/bin/env bash read -p "Please enter your choice: " response ## If the response given did not consist ...


14

Brevity vs. Readability: A Middle Ground As you've seen, this problem admits to solutions that are moderately long and somewhat repetitive but highly readable (terdon's and A.B.'s bash answers), as well as those that are very short but non-intuitive and much less self-documenting (Tim's python and bash answers and glenn jackman's perl answer). All these ...


14

Use that script: #!/bin/bash chars="/-\|" while :; do for (( i=0; i<${#chars}; i++ )); do sleep 0.5 echo -en "${chars:$i:1}" "\r" done done The while loop runs infinite. The for loop runs trough each character of the string given in $chars. echo prints the character, with a carriage return \r, but without linebreak -n. -e forces echo to ...


9

#!/bin/bash while true do read -p "Please enter your choice: " choice case "$choice" in [0-9]|[1-5][0-9]) echo "F" ;; 6[0-9]) echo "D" ;; 7[0-9]) echo "C" ;; 8[0-9]) echo "B" ;; 9[0-9]|100) echo "A" ;; [Qq]) ...


7

All Ubuntu installs have Python, so here is a python script one liner. If you need it to be in bash, I've also written the equivalent as a shell script. print (chr(75-max(5,int('0'+raw_input('Enter the number: ')[:-1])))) To run, save it in a file (e.g. grade.py) and then run it in terminal with this: python grade.py This is what you will see: Enter ...


6

After making it in Python 2, I decided to make it in bash. #! /bin/bash read -p "Enter the number: " i i=0$i x=$((10#${i::-1})) printf "\x$(printf %x $((11-($x>5?$x:5)+64)))\n" To run, save it in a file (e.g. grade.sh), make it executable with chmod +x grade.sh and then run with ./grade.sh. This is what you will see: Enter the number: 65 E How ...


6

Here's an example using \b, which tells the terminal emulator to move the cursor one column to the left, in order to keep overwriting the same character over and over. #!/usr/bin/env bash spinner() { local i sp n sp='/-\|' n=${#sp} printf ' ' while sleep 0.1; do printf '\b%s' "${sp:i++%n:1}" done } printf 'Doing important ...


6

This bash script will do that: run it with bash script.sh <path> or ./script.sh <path>, where <path> is the path to the folder containing the files: #!/bin/bash for path in ${1}/* do if [ -f "${path}" ] then extension="$(<<< "${file}" sed -rn 's/^.*\.(.*)$/\1/p')" if [ -n "${extension}" ] then ...


6

Here's another way: mkdir -p unknown; for f in *; do [[ $f =~ \. ]] && mkdir -p "${f##*.}" && mv "$f" "${f##*.}"/ || mv "$f" unknown/; done Explanation && means "run the command on the right only if the command on the left was successful". || means "run the command on the right only if the command on the left ...


5

Whether or not anything was found, find always returns true. You can use grep to determine if find found something: read -r a if find . -maxdepth 1 -name "$a" -print -quit | grep -q . then echo "You found the file" else echo "You haven't found the file" fi Quitting after the first match (-print -quit) should improve performance, as Eliah has noted. ...


4

Your sed invokation is wrong. sed 's/$host = 127.0.0.1/$host = localhost/' "$i" See the manual page of sed: sed [OPTION]... {script-only-if-no-other-script} [input-file] The inputfile is the last argument, not hte first. Interessting sidenote: t in sed is called a label. After t the name of the label follows. In your case it's the input filename ...


4

And here is my awk version: awk '{ if($_ <= 100 && $_ >= 0) { sub(/^([0-9]|[1-5][0-9])$/, "F", $_); sub(/^(6[0-9])$/, "D", $_); sub(/^(7[0-9])$/, "C", $_); sub(/^(8[0-9])$/, "B", $_); sub(/^(9[0-9]|100)$/, "A", $_); print } else { print "Only numbers between 0..100" } }' - or as ...


4

Here's my semi-esoteric bash solution, which populates an array with 101 entries and then checks user input against them. Even for real-world use that's reasonable--if you needed excellent performance you wouldn't be using bash, and a hundred (or so) assignments is still fast. But it would stop being reasonable if extended to a way bigger range (like a ...


4

Your original script has a (( i=1; i <= 5; i++ )) construct in for loop which is a bashism and hence is not being understood by dash. In Ubuntu sh is a symbolic link to dash, so when you are running sh ./script.sh you are basically running dash ./script.sh As i mentioned earlier dash is not understanding the C-like for loop construct, it is showing ...


4

Use bash parameter expansion: zip_file="${filename}" new_name="${zip_file%.*}" new_name will contain the name test if the zip_file has test.zip If the zip_file has test.foo.zip, new_name will have test.foo, if you want only test out of test.foo.zip use: new_name="${zip_file%%.*}"


2

I do not understand why that is required for Tomcat. All configuratios are in eg: /etc/tomcat7/server.xml. I have worked for several years with Tomcat, but this kind of configuration I've never needed. Anyway here is your answer. TL;DR env "host.name=localhost" Test with: env "host.name=localhost" perl -le 'print $ENV{"host.name"}' or with Java: ...


2

You can export them like this. #!/bin/bash #Specific Set Variables SpecficStatus="Pass" #SetVariable timestamp() { date +"%a %d %b %Y %T %Z"; } SpecficDate=$(timestamp) echo $SpecficStatus echo $SpecficDate #Properties Call file="savedState.properties" #Echo out the file while IFS== read -r VAR1 VAR2 do #echo "VAR1=$VAR1 : VAR2=$VAR2" export ...


2

Here's another "esoteric" answer perl -E ' print "number: "; $n = <>; say qw/A A B C D E F F F F F/[11-($n+1)/10] if $n=~/^\s*\d/ and 0<=$n and $n<=100 ' Explanation perl -E : the -E, like -e, allows passing a script as a command line argument. This is a way to run perl one-liners. Unlike -e, -E also enables all optional ...


2

If don't have to use the find command, using the test command (or its short form [...]) would be easier, IMHO. With test, the e switch does what you're looking for. #!/bin/bash read -r a if [[ -e $a ]]; then echo "You found the file" else echo "You haven't found the file" fi But be aware that test only looks for the file in the current directory, ...


2

I've found in the past that stringing the commands together in that manner causes issues if one of them have a "hiccup". For example, if your command is similar to this: sudo apt-get install 1 2 3 4 then, if there's an error on program 3, 4 will not install. Keeping them separate on each line fixes this issue. if there's an error, the script continues ...


2

Thought I'd contribute to the discussion a little bit. It's already been mentioned that the syntax used in your script is a bashism and hence isn't portable, even though this is C-like and is well understood by those familiar with java and C. From asking my own question on unix.stackexchange.com , I've also learned that syntax such as for i in $(seq 1 5) ...


1

Here's one way to get the gid, uid, shell and directory: printf "Enter username: " read user groupid=$( id -g $user ) userid=$( id -u $user ) usershell=$( grep $user /etc/passwd | awk -F':' '{ print $7 }' ) userdirectory=$( grep $user /etc/passwd | awk -F':' '{ print $6 }' )


1

The reason why your sed is not working was already given by chaos's good answer. Let me go a bit step further to indicate another way to loop through your file, since you are using some unnecessary steps: while IFS= read -r file do echo "$file" if grep -q "public \$host = '127.0.0.1';" "$file"; then echo "there's config file" sed ...


1

As heemayl's answer points out, your construction is a bashism. The standard way of doing what you want would be: for i in 1 2 3 4 5 do echo "Random number $i: $RANDOM" done Or, more easily expandably: for i in $(seq 1 5) do echo "Random number $i: $RANDOM" done (See man seq for more detail).


1

Thanks a lot for your reply AB. However i found a much better solution. I simply added in the CATALINA_OPTS variable of tomcat in the file /usr/share/tomcat7/bin/setenv.sh: export CATALINA_OPTS="-Dhost.name=localhost" This worked exactly as I wanted.


1

this one works on my bq aquaris 4.5: [Desktop Entry] Version=1.0 Type=Application Terminal=false Exec=aa-exec -- ssh -T pi@raspi2 francebleu.sh Icon=/home/phablet/.local/share/icons/francebleustart.png Name=FranceBleu Start X-Ubuntu-Touch=true X-Ubuntu-Default-Department-ID=accessories


1

heemayl is still right: example: full_path=/foo/bar/baz.zip file_name="${full_path##*/}" name="${file_name%.*}"


1

I created this from your other question as well. It took me a bit, but this is what I was able to come up with to create the folders based on the zip file name, removing the .zip from the folder name, then extracting the zip file into that folder. #!/bin/bash echo "Start folder create..." find . -type f -name "*.zip" | while read filename do ...


1

You can accomplish this with some udev rules, as @Rinzwind suggested. First, save your script to somewhere accessible by other users. For example, you could save it as /usr/local/bin/script.sh. Now, make the script accessible by others. sudo chmod 555 /usr/local/bin/script.sh Now, plug in your USB modem and then run lsusb. Below is an example output: $ ...


1

TL;DR The script can not work because that shortcut will be sent to the active window, and that's not chromium-browser with Streamus This is your reload xdotool command xdotool search --limit 1 --name "^Streamus$" | xargs -I {} xdotool windowactivate --sync {} key ctrl+r The complete command in your case is: /usr/bin/chromium-browser --disable-gpu ...



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