Shell scripting
Shell scripting
#!/bin/bash
DIR=$1
for f in $DIR/*.c; do
echo $f
done
sum=0
:w
for f in $DIR/*.c; do
n_lines=`grep -E -c "[^ ]" $f`
echo "$f has $n_lines of C code"
sum=`expr $sum + $n_lines`
done
#!/bin/bash
#Read the console input until the user provides a filename that exists and
can be read
F=""
while test -z $F || ! test -f $F || ! test -r $F; do
read -p "Give a readable filename:" F
done
#!/bin/bash
# Receive filenames as cmd line args and print them sorted in reverse order
of their actual size on disk
for f in $@; do
if [ -f $f ]; then
du -b $f
fi
done | sort -n -r
#!/bin/bash
#!/bin/bash
#
# Doing simple math in Bash
#
# Shell script variables are by default treated as strings, not numbers.
#
#!/bin/bash
F=””
while [ -z "$F" ] || [ ! -f "$F" ] || [ ! -r "$F" ]
do
read -p "Provide an existing and readable file path:" F
done
#!/bin/bash
# Find all the files in the directory given as first command line argument,
# larger in size than the second command line argument
D=$1
S=$2
#!/bin/bash
if [ $# -eq 0 ]
then
echo "Error: You must enter a filename."
exit 1
elif !(test -r $1)
then
echo "File $1 does not exist."
exit 1
fi
#!/bin/bash
#!/bin/bash
for N in $NAMES
do
echo $N
done
#!/bin/sh
if [ $# -eq 0 ]
then
echo "Error: Not enough arguments."
exit 1
fi
for arg in $*
do
echo $arg
done
#!/bin/bash
# Count all the lines of code of the C files in the directory given as
command line argument,
# excluding lines that are empty or contain only blank spaces.
S=0
for F in $1/*.c
do
N=`grep "[^ \t]" $F | wc -l`
S=`expr $S + $N`
done
echo $S
#!/bin/bash
for A in $@
do
if test -f $A
then
echo $A is a file
elif test -d $A
then
echo $A is a dir
elif echo $A | grep -q "^[0-9]\+$"; then
echo $A is a number
else
echo We do not know what $A is
fi
done
#!/bin/bash
for A in $@
do
if [ -f $A ]
then
echo $A is a file
elif [ -d $A ]
then
echo $A is a dir
elif echo $A | grep -q "^[0-9]\+$"
then
echo $A is a number
else
echo We do not know what $A is
fi
done
#!/bin/bash
# Sa se scrie un script bash care primeste ca argument un nr natural N
done
exit 0
#!/bin/bash
if [ $# -eq 0 ]; then
echo 'You must supply a directory name'
exit 1
fi
if [ ! -d $1 ]; then
echo "$1 is not a directory"
exit 2
fi
#!/bin/bash
while [ $# -ne 0 ]; do
FILE=$1
WORD=$2
NUM=$3
echo $FILE $WORD $NUM
shift 3
done