0% found this document useful (0 votes)
4 views

Shell scripting

The document contains multiple shell scripts that perform various tasks such as counting lines of C code in a directory, reading user input for filenames, sorting files by size, and performing basic arithmetic operations. Each script includes error handling for invalid inputs and demonstrates the use of loops, conditionals, and file operations in Bash. Overall, the document serves as a collection of practical examples for Bash scripting.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Shell scripting

The document contains multiple shell scripts that perform various tasks such as counting lines of C code in a directory, reading user input for filenames, sorting files by size, and performing basic arithmetic operations. Each script includes error handling for invalid inputs and demonstrates the use of loops, conditionals, and file operations in Bash. Overall, the document serves as a collection of practical examples for Bash scripting.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Shell scripting

#!/bin/bash

if test $# -lt 1; then


echo "Script needs one argument."
exit 1
fi

DIR=$1

if ! test -d $DIR; then


echo "First parameter must be a directory."
exit 1
fi

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

echo "In total we have $sum lines."


echo "Today is `date` "

#!/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

echo $1 is the first argument


echo $# is the number of arguments
echo $@ is the list of arguments

for arg in $@; do


if test -f $arg; then
echo $arg is a file
elif [ -d $arg ]; then
echo $arg is a directory
elif [ $arg -eq $arg ]; then
echo $arg is an integer
else
echo $arg is a random string
fi
done

#!/bin/bash

echo "Filename: $0"


echo "First argument: $1"
echo "Second argument: $2"
echo "Argument count: $#"
echo "Argument list: $@"
echo "Argument list: $*"
#!/bin/bash

#
# Doing simple math in Bash
#
# Shell script variables are by default treated as strings, not numbers.
#

# read the first number


echo -n "First number: "
read num1

# read the second number


echo -n "Second number: "
read num2

# 1. Using compound command (( expresion ))


sum=$((num1+num2))

# 2. Using let built-in command


#let sum=num1+num2

# 3. Using expr extern command


#sum=`expr $num1 + $num2` # put spaces around + sign

# 4. Using declare built-in command


#declare -i sum
#sum=num1+num2

echo "The result is:" $sum

#!/bin/bash

# Read the console input until the user


# provides a filename that exists and can be read

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

find $D -type f | while read F


do
N=`ls -l $F | awk '{print $5}'`
if test $N -gt $S
then
echo $F
fi
done

#!/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

while read -r line


do
echo $line
done

#!/bin/bash

echo -n "Enter a filename: "


read filename

while read -r line


do
echo $line
done < $filename

#!/bin/bash

NAMES='Ana Iulia Maria Tudor'

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

for X in $(seq 1 $1); do


#create a file
touch "file_$X.txt"
#write content to the file
sed -E -n ''$X',+5p' /etc/passwd > "file_$X.txt"

done
exit 0

#!/bin/bash

#check the arguments

if [ $# -eq 0 ]; then
echo 'You must supply a directory name'
exit 1
fi

#validate the argument

if [ ! -d $1 ]; then
echo "$1 is not a directory"
exit 2
fi

for FILE in $(find $1 -type f); do


if file $FILE | grep -E -q ":ASCII text$"; then
NL=$(wc -l < $FILE)
if [ $NL -lt 6 ]; then
cat $FILE
else
head -3 $FILE
tail -3 $FILE
fi
fi
done

#!/bin/bash

# ./ex3.sh file1 mere 2 file2 pere 1

while [ $# -ne 0 ]; do
FILE=$1
WORD=$2
NUM=$3
echo $FILE $WORD $NUM
shift 3

done

You might also like