Distinguish between command line parameter passing exercises and user input parameters

1,

Create a file named foo.sh Script for , Let it provide the following features  

A. When running /root/foo.sh redhat, Output as fedora 

B. When running /root/foo.sh fedora, Output as redhat 

C. When there is no parameter or the parameter is not redhat perhaps fedora Time , Prompt to output the following information :/root/foo.sh redhat|fedora

no need bash Method of program name , It is required to write clearly how to implement it .

touch foo.sh

vi foo.sh

#!/bin/bash

if [ "$1" = redhat ];

then echo 'fedora'

elif [ "$1" = fedora ];

then echo 'redhat'

else echo "/root/foo.sh redhat|fedora"

fi

:wq

chmod 755 foo.sh

./foo.sh redhat

  

2, Write a script file checkuser, When the script runs, it gets the user name entered by the user , stay /etc/passwd Find whether the user exists in the file . Tips :cut 
/etc/passwd -d: -f1

touch checkuser

vi checkuser

#!/bin/bash

echo ' enter one user name '

read username

i=`cut /etc/passwd -d: -f1 |grep -c $username`

if [ "$i" = "1" ];

then echo ' This user exists '

else echo ' This user does not exist '

fi

3.
Write a script to back up the files specified by the user ( How to get user input customization ), Back up the files to the directory under backup In the directory ( If the directory does not exist, it will be created automatically ), The file name format of the backup file is “ file name _bak_ specific date _ Hour, minute and second ”.

touch bak.sh

chmod 777 bak.sh

#!/bin/bash

echo ' enter path '

read filepath

dirs=`ls ~ | grep -c 'backup'`

if [ "$dirs" = "0" ];

then `mkdir backup`

fi

bakpath="/root/backup/"

if [ -f $filepath ];

then

cp $filepath $bakpath`basename $filepath`_bak_`date '+%Y%m%d_%H%M%S'`

else echo " file does not exist "

fi

 

practice case control structure

4. Write a shell Script program , It can take different actions according to the input command line parameters : If it is a directory , Then the files in the directory are listed ; If it is an executable file , Then use shell Executive ; If it is a readable file , Then the content is displayed in separate screens .

touch four.sh

Direct use if

#!/bin/bash

file=$1

if [ -d $file ];

then ls $file

elif [ -x $file ];

then sh $file

elif [ -r $file ];

then more $file

else ll $file

fi

Second case+if

#!/bin/bash

file=$1

n=0

if [ -d $file ];

then n=1

elif [ -x $file ];

then n=2

elif [ -r $file ];

then n=3

else n=4

fi

case $n in

1) ls $file;;

2) sh $file;;

3) more $file;;

4) ll $file;;

esac    

Practice loop control structure

5. All files in the given directory of the user mp3 File name change , The name is preceded by a number ( increase +1 that will do ). Require the user to pass the given directory read Input statement get .

Tips : $[],$(())

#!/bin/bash

echo ' Input directory '

read dirpath

files=`ls $dirpath/*.mp3`

num=1

for m in $files

do

mv "$dirpath/`basename $m`" "$dirpath/${num}`basename $m`"

num=$((num+1))

done

File directory operation script writing exercise

6. Under the home directory fd Backup directory data , Requires that you always keep the most recent 3 Backup times . The data to be backed up is stored in /home/user/workdata in , The last three backup files are named worknewdata.tar,work2nddata.tar and work3rddata.tar.

#!/bin/bash

num=`ls ${HOME}/workdata | grep -c "work*"`

change()

{

mv ~/workdata/work2nddata.tar ~/workdata/work3rddata.tar

mv ~/workdata/worknewdata.tar ~/workdata/work2nddata.tar

tar -Pczf ~/workdata/worknewdata.tar ~/fd

}

if [ $num -eq 3 ];

then

`rm -f ~/workdata/work3rddata.tar`

change

elif [ $num -eq 2 ];

then change

elif [ $num -eq 1 ];

then

mv ~/workdata/worknewdata.tar ~/workdata/work2nddata.tar

tar -cPzf ~/workdata/worknewdata.tar ~/fd

else tar -cPzf ~/workdata/worknewdata.tar ~/fd

fi

Technology