I am trying to write a two argument function in bash. here is the code
cd() {
if [ $1 != .. ]; then builtin cd $1 ; ls
else
if [ -z "$2" ]; then
builtin cd ..
else
echo "$2"
int= $2
while [ $int > 0 ]; do
builtin cd ..
((int=int-1))
done
fi ;
fi ;
}
when i run it in terminal with this line cd .. 4 it will cd the parent and echo 4 but then it prints 4: command not found. Could anyone help me with this?
Related
if [ "$#" -ne 1 ]; then
flag=1;
elif ! [[ "$1" == "arg1" || "$1" == "arg2" || "$1" == "arg3" || ...... ]]; then
echo "Invalid"
flag=1;
fi
if [ "$flag" == "1" ]; then
echo "Usage of script...."
exit
fi
count="$(ls *.mov | wc -l)"
if [[ "$count" -eq 0 ]]
then
echo there are 0 .mov files in this path
elif [[ "$count" -eq 1 ]]
then
echo there is 1 .mov file in this path
vlc *.mov
elif [[ $1 = "arg1" ]] ; then
echo entered the tough part....coz its not entering`enter code here`
elif [[ "$1" == "arg2" ]] || [[ "$1" == "arg10" ]] ; then
echo entered here atleast...but not entering
else
script continues
The code does not enter elif conditions involving command line arguments. Tried =, ==, -eq, double square braces, single square braces. But it does not enter, pls help
you should add a disclaimer at the head of the script: " #!/bin/bash"
I tried your script and it does get into the elif.
the code I used is:
if [ "$#" -ne 1 ]; then
echo "not 1 arg"
flag=1;
elif ! [[ "$1" == "arg1" || "$1" == "arg2" || "$1" == "arg3" ]]; then
echo "Invalid"
flag=1;
else
echo "else"
fi
and the input/output are:
$ . script.sh 1 2
not 1 arg
$ . script.sh 1
Invalid
$ . script.sh arg1
else
I tried the second part and it is also working:
count=$2
if [[ "$count" -eq 0 ]] ;then
echo "there are 0 .mov files in this path"
elif [[ "$count" -eq 1 ]] ;then
echo "there is 1 .mov file in this path"
vlc *.mov
elif [[ $1 = "arg1" ]] ; then
echo "arg1 "
elif [[ "$1" == "arg2" ]] || [[ "$1" == "arg10" ]] ; then
echo "arg2 or arg10 "
else
echo "else"
fi
and tested it (second argument is "count"):
$ . script.sh arg1 0
there are 0 .mov files in this path
$ . script.sh 1 0
there are 0 .mov files in this path
$ . script.sh arg10 0
there are 0 .mov files in this path
$ . script.sh arg1 1
there is 1 .mov file in this path
The program 'vlc' is currently not installed. To run 'vlc' please ask your administrator to install the package 'vlc-nox'
$ . script.sh arg10 1
there is 1 .mov file in this path
The program 'vlc' is currently not installed. To run 'vlc' please ask your administrator to install the package 'vlc-nox'
$ . script.sh 1 1
there is 1 .mov file in this path
The program 'vlc' is currently not installed. To run 'vlc' please ask your administrator to install the package 'vlc-nox'
$ . script.sh arg1 2
arg1
$ . script.sh 1 2
else
$ . script.sh arg10 2
arg2 or arg10
You should look into the bash case statement http://mywiki.wooledge.org/BashGuide/TestsAndConditionals#Choices_.28case_and_select.29
For this particular script, it will make your script easier to read and your branching problem will most likely go away..
if your purpose is to handle the arguments passed to the script by command line it's better to use the getopts bash
this is just an example that you can adapt to your scope:
#!/bin/bash
function usage {
echo "usage: ..."
}
while getopts f:o:h opt; do
case $opt in
f)
fileName=$OPTARG
echo "filename[$fileName]"
;;
o)
otherargs=$OPTARG
echo "otherargs[$otherargs]"
;;
h)
usage && exit 0
;;
?)
usage && exit 2
;;
esac
done
~
output
[myShell] ➤ ./n -h
usage: ...
[myShell] ➤ ./n -f myfilename
filename[myfilename]
[myShell] ➤ ./n -o other
otherargs[other]
[myShell] ➤ ./n -l
./n: illegal option -- l
usage: ...
# define some variables for later
date=`date`
usr=`whoami`
# define usage function to echo syntax if no args given
usage(){
echo "error: filename not specified"
echo "Usage: $0 filename directory/ directory/ directory/"
exit 1
}
# define copyall function
copyall() {
# local variable to take the first argument as file
local file="$1" dir
# shift to the next argument(s)
shift
# loop through the next argument(s) and copy $file to them
for dir in "$#"; do
cp -R "$file" "$dir"
done
}
# function to check if filename exists
# $f -> store argument passed to the script
file_exists(){
local f="$1"
[[ -f "$f" ]] && return 0 || return 1
}
# call usage() function to print out syntax
[[ $# -eq 0 ]] && usage
here's what I can't figure out
# call file_exists() and copyall() to do the dirty work
if ( file_exists "$1" ) then
copyall
I would also love to figure out how to take this next echo section and condense it to one line. Instead of $1 then shift then move on. Maybe split it into an array?
echo "copyall: File $1 was copied to"
shift
echo "$# on $date by $usr"
else
echo "Filename not found"
fi
exit 0
It seems to me that the file_exists macro is superfluous:
if [ -f "$1" ]
then copy_all "$#"
else echo "$0: no such file as $1" >&2; exit 1
fi
or even just:
[ -f "$1" ] && copy_all "$#"
You probably just need to remove the parentheses around file_exists "$1", and add a semicolon:
if file_exists "$1"; then
copyall
I just think that it is convenient for me to "cd" to the directory where I store some file, ie.
[admin#local /]$ cd /usr/bin/somefile.pl
which as far as I know that the official "cd" command will not work.
so I wrote something like this:
main () {
if [[ "${1}" =~ "(.+/)*(.*){1}" ]] && [ -f "${1}" ] ; then
`\cd ${1%/*}`
elif [ -f "${1}" ] ; then
exit 0
else ; `\cd ${1}`
fi
}
main ${1}
and I alias this cd.sh to the "cd" command:
alias cd='source /somepath/cd.sh'
and this doesn't work.
I've tried to use eval "\cd xxx" instead of just \cd xxx;
How can I fix my script?
It feels like a bad idea to override cd, so I'll suggest a slightly different command, fcd:
fcd() { cd -- "$(dirname -- "$1")"; }
$ fcd /usr/bin/somefile.pl
$ pwd
/usr/bin
Or using parameter expansion to save a call to dirname:
fcd { cd -- "${1%/*}"; }
cd() {
DN="$(dirname "$1")"
if [[ -d "$1" ]]; then
builtin cd "$1"
elif [[ -d "$DN" ]]; then
builtin cd "$DN"
else
echo "$* or $DN: No such directories"
return 1
fi
return 0
}
Often when writing for the bash shell, one needs to test if a file (or Directory) exists (or doesn't exist) and take appropriate action. Most common amongst these test are...
-e - file exists, -f - file is a regular file (not a directory or device file), -s - file is not zero size, -d - file is a directory, -r - file has read permission, -w - file has write, or -x execute permission (for the user running the test)
This is easily confirmed as demonstrated on this user-writable directory....
#/bin/bash
if [ -f "/Library/Application Support" ]; then
echo 'YES SIR -f is fine'
else echo 'no -f for you'
fi
if [ -w "/Library/Application Support" ]; then
echo 'YES SIR -w is fine'
else echo 'no -w for you'
fi
if [ -d "/Library/Application Support" ]; then
echo 'YES SIR -d is fine'
else echo 'no -d for you'
fi
➝ no -f for you ✓
➝ YES SIR -w is fine ✓
➝ YES SIR -d is fine ✓
My question, although seemingly obvious, and unlikely to be impossible - is how to simply combine these tests, without having to perform them separately for each condition... Unfortunately...
if [ -wd "/Library/Application Support" ]
▶ -wd: unary operator expected
if [ -w | -d "/Library/Application Support" ]
▶ [: missing `]'
▶ -d: command not found
if [ -w [ -d "/Library.... ]] & if [ -w && -d "/Library.... ]
▶ [: missing `]'
➝ no -wd for you ✖
➝ no -w | -d for you ✖
➝ no [ -w [ -d .. ]] for you ✖
➝ no -w && -d for you ✖
What am I missing here?
You can use logical operators to multiple conditions, e.g. -a for AND:
MYFILE=/tmp/data.bin
if [ -f "$MYFILE" -a -r "$MYFILE" -a -w "$MYFILE" ]; then
#do stuff
fi
unset MYFILE
Of course, you need to use AND somehow as Kerrek(+1) and Ben(+1) pointed it out. You can do in in few different ways. Here is an ala-microbenchmark results for few methods:
Most portable and readable way:
$ time for i in $(seq 100000); do [ 1 = 1 ] && [ 2 = 2 ] && [ 3 = 3 ]; done
real 0m2.583s
still portable, less readable, faster:
$ time for i in $(seq 100000); do [ 1 = 1 -a 2 = 2 -a 3 = 3 ]; done
real 0m1.681s
bashism, but readable and faster
$ time for i in $(seq 100000); do [[ 1 = 1 ]] && [[ 2 = 2 ]] && [[ 3 = 3 ]]; done
real 0m1.285s
bashism, but quite readable, and fastest.
$ time for i in $(seq 100000); do [[ 1 = 1 && 2 = 2 && 3 = 3 ]]; done
real 0m0.934s
Note, that in bash, "[" is a builtin, so bash is using internal command not a symlink to /usr/bin/test exacutable. The "[[" is a bash keyword. So the slowest possible way will be:
time for i in $(seq 100000); do /usr/bin/\[ 1 = 1 ] && /usr/bin/\[ 2 = 2 ] && /usr/bin/\[ 3 = 3 ]; done
real 14m8.678s
You want -a as in -f foo -a -d foo (actually that test would be false, but you get the idea).
You were close with & you just needed && as in [ -f foo ] && [ -d foo ] although that runs multiple commands rather than one.
Here is a manual page for test which is the command that [ is a link to. Modern implementations of test have a lot more features (along with the shell-builtin version [[ which is documented in your shell's manpage).
check-file(){
while [[ ${#} -gt 0 ]]; do
case $1 in
fxrsw) [[ -f "$2" && -x "$2" && -r "$2" && -s "$2" && -w "$2" ]] || return 1 ;;
fxrs) [[ -f "$2" && -x "$2" && -r "$2" && -s "$2" ]] || return 1 ;;
fxr) [[ -f "$2" && -x "$2" && -r "$2" ]] || return 1 ;;
fr) [[ -f "$2" && -r "$2" ]] || return 1 ;;
fx) [[ -f "$2" && -x "$2" ]] || return 1 ;;
fe) [[ -f "$2" && -e "$2" ]] || return 1 ;;
hf) [[ -h "$2" && -f "$2" ]] || return 1 ;;
*) [[ -e "$1" ]] || return 1 ;;
esac
shift
done
}
check-file fxr "/path/file" && echo "is valid"
check-file hf "/path/folder/symlink" || { echo "Fatal error cant validate symlink"; exit 1; }
check-file fe "file.txt" || touch "file.txt" && ln -s "${HOME}/file.txt" "/docs/file.txt" && check-file hf "/docs/file.txt" || exit 1
if check-file fxrsw "${HOME}"; then
echo "Your home is your home from the looks of it."
else
echo "You infected your own home."
fi
Why not write a function to do it?
check_file () {
local FLAGS=$1
local PATH=$2
if [ -z "$PATH" ] ; then
if [ -z "$FLAGS" ] ; then
echo "check_file: must specify at least a path" >&2
exit 1
fi
PATH=$FLAGS
FLAGS=-e
fi
FLAGS=${FLAGS#-}
while [ -n "$FLAGS" ] ; do
local FLAG=`printf "%c" "$FLAGS"`
if [ ! -$FLAG $PATH ] ; then false; return; fi
FLAGS=${FLAGS#?}
done
true
}
Then just use it like:
for path in / /etc /etc/passwd /bin/bash
{
if check_file -dx $path ; then
echo "$path is a directory and executable"
else
echo "$path is not a directory or not executable"
fi
}
And you should get:
/ is a directory and executable
/etc is a directory and executable
/etc/passwd is not a directory or not executable
/bin/bash is not a directory or not executable
This seems to work (notice the double brackets):
#!/bin/bash
if [[ -fwd "/Library/Application Support" ]]
then
echo 'YES SIR -f -w -d are fine'
else
echo 'no -f or -w or -d for you'
fi
I'm having a hard time figuring out how to do this if statement. I want to do this:
IF (the function has only 1 argument
AND $1 is a directory (in the current
folder)) OR IF (the function has 2
arguments AND $1 is NOT a directory ) THEN
....
END
Sorry if it's not very clear,
Thanks in advance
You pretty much said what is needed in the question:
if [ $# = 1 -a -d "$1" ] || [ $# = 2 -a ! -d "$1" ]
then
...
fi
You can use other operators too - that will work in Bourne shell, let alone Korn or other POSIX shells (assuming that $# works correctly in a function - which it does in Bash).
fun()
{
if [ $# = 1 -a -d "$1" ] || [ $# = 2 -a ! -d "$1" ]
then echo "Pass ($1, $#)"
else echo "Fail ($1, $#)"
fi
}
fun $HOME
fun $HOME abc
fun $HOME/xyz abc
fun /dev/null
if [[ $# == 1 && -d "$1" ]]; then
echo "one param, first is a dir"
elif [[ $# == 2 && ! -d "$1" ]]; then
echo "two params, first is not a dir"
else
echo "unexpected"
fi