5 CPS393 LinuxOddsEnds
5 CPS393 LinuxOddsEnds
Linux
Odds & Ends
tmp is empty
Hits EOF immediately
Options:
Words VS Lines
read arg1 arg2 rest
First word is read into arg1, second word into arg2, the rest of the line into rest
read
var="hi there"
echo $var | read x y
echo $x $y #prints nothing!!
hello
world
ctrl-d
#!/bin/bash
#Source: shiftex
#pgm to print its args.
while [ "$1" ] #<--- stops when $1 is null
do
echo "$1" #<--- so spaces preserved
shift #<--- moves $2 to $1, $3 to $2 etc.
done
exit 0
Example: List lines containing 'bash' from all files whose NAME
starts with 'f', where files are located anywhere in the filetree
under current directory
Example: List lines containing 'bash' from all files whose NAME
starts with 'f', where files are located anywhere in the filetree
under current directory
By type: By name:
Find in file starting Output piped into xargs,
current which calls grep with that
directory output as args to grep
Example: List lines containing 'bash' from all files whose NAME
starts with 'f', where files are located anywhere in the filetree
under current directory
-VS-
find . -type f -name "f*" | grep bash
ls | xargs
If no command is given to xargs, default is echo
If current dir contains files a, b, c
What ends up running is echo a b c
Another example:
Note: If there are directories, the copy will fail with a msg
to stderr (can of course redirect 2>/dev/null).
Xargs then just continues with the next item.
#!/bin/bash
# Source: printXth
echo -n "enter an integer: "
read X
the_arg='$'${X}
echo "arg number ${X} is: ${the_arg}"
eval echo "arg number ${X} is: ${the_arg}"
echo is both!
The shell recognizes it as a built-in, but the
version of echo that gets called is in /bin
Test this using which:
PATH=${PATH}:${HOME}/bin
PATH=${PATH}:${HOME}/bin
export PATH
For example:
if you write your own rm in ~userid/bin/rm
to use instead of /bin/rm
you must put YOUR ~userid/bin before /bin
in PATH, or /bin/rm will always be executed.
PATH=${PATH}:${HOME}/bin
-VS-
PATH=${HOME}/bin:${PATH}
a=Hello
c=123 →
string containing
digit
123
declare i c=123 →
integer
Problem:
Referencing a variable before it is assigned a value (programmer error)
It will be null, and the shell program using it may behave unexpectedly
fname.
Probably not what you wanted
${ var:=value }
Place in a script. If variable is not set, it will have the default value.
If the variable was set and exported, it will have set value.
[
if $temp
""
"
•, ,
↳ win naming
ad .
another
way command
test is
if [ -
cd $temp
rm *
cd ..
rmdir $temp
fi
while [ true ]
do Use string between here tags as input to cat.
clear Notice the << redirection
cat << here
MAIN MENU
1) Print working dir.
2) List all entries in current dir.
3) Print date & time
4) Display contents of file
X) Exit
here
case $selection in
while [ true ] 1) pwd ;;
do
2) ls ;;
clear
cat << here 3) date ;;
MAIN MENU 4) echo -e "Enter a file name: \c"
file
1) Print working dir. read fname
a
.
it is
it
-
2) List all entries in current dir.
if [ -f $fname ]; then
3) Print date & time
4) Display contents of file echo "Contents of $fname are: "
X) Exit more $fname
here else
echo "file $fname does not exist"
fi ;;
X) exit 0 ;;
*) echo -e "Invalid choice. Try again \a" ;;
esac
echo -e "\n Press return to continue \c"
read hold
done
© Alex Ufkes, 2022 exit 0 56
© Alex Ufkes, 2022 57
© Alex Ufkes, 2022 58
© Alex Ufkes, 2022 59