Showing posts with label shell. Show all posts
Showing posts with label shell. Show all posts

Friday, October 11, 2013

Everyday Shell Scripts

  • Rename the extensions of all files,

    for FILE in *.foo ; 
    do NEWFILE=`echo $FILE | sed 's/.foo/.bar/g'` ;
     mv "$FILE" $NEWFILE ; 
    done
  • Test if a variable is set (pay attention to the space, they can't be eliminated),

    if [ ! -z "$var" ]; then
      echo "var exists"
    fi
  • Test if a file exists,

    if [ -f "$file" ]; then
      echo "file exists"
    fi
  • Copy one file to the clipboard (install xclip first)

    cat file | xclip -selection c
  • Paste clipboard to a file

    xclip -o > file
  • Define a function

    foo(){
      echo "FUNCTION"
    }
    fun=$(foo)
    
  • The most recent foreground pipeline exit status: $?

    if [ $? -eq -1 ]
      then
      exit 1
    fi 
  • Several ways of for loop controlled by a var

    for I in {1..10}; do echo $I; done
    for I in 1 2 3 4 5 6 7 8 9 10; do echo $I; done
    for I in $(seq 1 10); do echo $I; done
    for ((I=1; I <= 10 ; I++)); do echo $I; done
  • Generate ticks in (-4pi, 4pi):

    for i in $(seq -4 4); do b=`echo "$i * 3.14159" | bc`; echo -ne $b","; done; 

    This is useful for PGFplots for trigonometric ticks,

    xtick={-12.56636,-9.42477,-6.28318,-3.14159,0,
    3.14159,6.28318,9.42477,12.56636},
    xticklabels={
            $-4\pi$, $-3\pi$, $-2\pi$, $-\pi$, $0$,
            $\pi$, $2\pi$, $3\pi$, $4\pi$
     }
  • Convert all jpeg files and save them as png files.

    for file in *.jpg;do 
      newfile=`echo $file | sed 's/.jpg/.png/g'`;
      gm convert -quality 60 $file $newfile;
    done
  • Compress all jpeg files.

    mkdir new
    for file in *.jpg;do 
      newfile="new/$file";
      gm convert -quality 60 $file $newfile;
    done

Friday, May 24, 2013

Install terminal and gcc on Android

Always read instructions carefully.

- #1 note on a survival manual, Yann Martel Life of Pi

On Android, everything is an app.

- mzt

Shell for Android is an app too. Install Terminal IDE (210MB) from the app store. Yes, this is not built-in, not default, also with limited use: vi, javac, gcc, git, ssh etc. It runs without root permission. After the installation, switch on its ugly but complete keyboard to get the full function use.

Install gcc

Under Terminal IDE, gcc is paralleled by terminal-gcc, which is not installed by default (due to the volume of gcc). If you go to help file from its main menu, you will see a series of tutorials explaining how to start making the best use of it. Here I just quote the part on gcc. On any of its terminal window (there are 4),

# cat `which install_gcc`
# install_gcc

After installation, use the following command to compile,

# terminal-gcc foo.c -o foo.o

Monday, April 1, 2013

shell: Rename Multiple Files in a folder

Substitute foo in file names to bar:

for f in *;
do 
  mv $f ${f/foo/bar}
done

Prefix all .jpg files with foo and a counter:

i=1
for f in *.jpg;
do
  mv $f 'foo-'$i'.jpg'
  i=`expr $i + 1`
done

Notice spaces in expr can not be omitted.

Windows: MINGW32

You can start a MINGW32 shell command line from git cmd under Windows.

i=0;for f in *.jpg; do mv $f $i'.jpg'; i=`expr $i + 1`; done

Tuesday, March 12, 2013

Dollars in Shell

As pointed out here (stackoverflow : link):
  • Positional parameters $1 ,2,3… and their corresponding array representation, count and IFS expansion @, #, and *.
  • - current options set for the shell.
  •  pid of the current shell (not subshell)
  • _ most recent parameter (or the abs path of the command to start the current shell immediately after startup)
  • IFS the (input) field separator
  • ? most recent foreground pipeline exit status
  • ! PID of the most recent background command
  • 0 name of the shell or shell script