I want to make a multiple flags for my script. And didn't work like what I want it.
#!/bin/bash
function wherecpy
{
echo "Plaes write where the files are located: "
read placefile
cd $placefile
}
function funcpy
{
wherecpy
echo ""
echo "Please choose the following:"
echo ""
echo "1. Interactive copy, answer yes or no (y/n) before doing the copy"
echo "2. Make backups of existing destination files"
echo "3. Preserve file attributes"
echo "4. Do a recursively copy"
echo "5. Back to main menu"
echo ""
echo -n "Enter Your Selection: "
read selection
echo ""
case $selection in
1) a="-i" ;;
2) b="--backup" ;;
3) c="--preserve=all" ;;
4) d="-r" ;;
0) return 0 ;;
esac
echo "Type the name of the file you wish to copy/backup: "
read file
echo "Type the name of the destination file/directory: "
read dest
cp $a $b $c $d "$file" "$dest"
if [ $? == 0 ]; then
echo "Success"
else
echo "failed."
fi
}
I want it to work like that :
Please type in the source file(s) to copy:
File1 file2 file3
Please type in the destination:
dir1
Copy successful.
Press any key to return to main menu.
Use getopts :
There are many online resources you can take reference from
Related
I am trying to make a bash script that gives the user options 1-5,with 5 being to exit. For option 2 I want the to be able to pass the file name as a argument for the file_size script but even when I catch the user input it still says the error message please enter a single file name as an argument. I am not sure why is doesn't pass it as a argument, Any help would be appreciated.
#!/bin/bash
# usage: ./menu_script
exitvar=false
while [ $exitvar = "false" ] # keep looping until exit equal true
do
echo -n "Enter a number 1 through 5, 5 to exit"
read number
case $number in
1) echo "Move empty files"
/home/student/move_empty;;
2) echo "Check file size"
echo -n "Enter a file name to check: "
read sourcefile
/home/student/file_size;;
3) echo "Option3";;
4) echo "File check rwx"
echo -n "Enter a file name to check: "
read sourcefile
if [ -e $sourcefile ] # does the source file exist?
then
echo $sourcefile "exists!"
fi
# find out if file has write permission or not
[ -w $sourcefile ] && W="Write = yes" || W="Write = No"
# find out if file has excute permission or not
[ -x $sourcefile ] && X="Execute = yes" || X="Execute = No"
# find out if file has read permission or not
[ -r $sourcefile ] && R="Read = yes" || R="Read = No"
echo "$sourcefile permissions"
echo "$W"
echo "$R"
echo "$X"
;;
5) echo "exit"
exitvar="true"
;;
*) echo "Invalid number try again"
;;
esac
done
At the option 2)
2) echo "Check file size"
echo -n "Enter a file name to check: "
read -r sourcefile
echo /home/student/file_size "$sourcefile";;
To see if the argument is being pass to /home/student/filesize
It would be nice and informative to show a menu with numbers alongside the option.
Or run subscripts with source or . this way subscripts will gain access to vars in main script
source /home/student/file_size
. /home/student/file_size
And use -p option for read to minimize echoes
read -r -p "Enter a file name to check: " sourcefile
I commented the things I have problem.
and also, is there any other way I can exit my loop without using the exit command?.................
#!/bin/bash
while [ "$done" != "true" ] #this don't work
do
echo "Please enter one of the following options"
echo "1. Move empty files"
echo "2. Check file size"
echo "3. Which file is newer"
echo "4. File check rwx"
echo "5. Exit".
echo -n "Enter Choice: "
read scale # starting from this part for checking if user only inputs numbers 1-5 not working
if ! [[ "$scale" =~ ^[0-6]+$ ]]
then
echo "Invalid Input"
fi #up to this part is not working
read -r answer
case "$answer" in
1) ./move_empty
exit 55
;;
2) ./file_size
exit 0
;;
3) ;;
;;
4)
;;
5) done="true";;
esac
done
There are few things Which should be avoided
while [ "$done" != "true" ] --> while [ $done -ne 1 ] /* -ne is not equal to */
case "$answer" in --> case $answer in
Complete Code
var=0
flag=1
while [ $var -ne 1 ]
do
echo "Please enter one of the following options"
echo "1. Move empty files"
echo "2. Check file size"
echo "3. Which file is newer"
echo "4. File check rwx"
echo "5. Exit".
echo -n "Enter Choice: "
read scale
if [ $scale -gt 0 -a $scale -lt 6 ]
then
echo "valid Input, you can procees for switch "
else
echo "invalid input.. go again & give correct one "
flag=0
fi
if [ $flag -eq 1 ]
then
read -r answer
case $answer in
1) ./move_empty
#exit 55
break
;;
2) ./file_size
#exit 0
break
;;
3)
exit 0
;;
4)
;;
5) done="true";;
esac
fi
done
To come out from loop, without using exit command, use break. Here is the answer from man 1 bash
break [n]
Exit from within a for, while, until, or select loop.
If n is specified, break n levels. n must be ≥ 1. If n
is greater than the number of enclosing loops, all
enclosing loops are exited. The return value is 0
unless n is not greater than or equal to 1.
I am new to Linux bash scripting and I can't seem to find what I'm doing wrong. Here's my code. Entering number 2 and 3, after the prompt that I ask the user my code stops it doesn't continue to the IF ELSE statements. Thank you to those who will help!
#!/bin/bash
while true
do
clear
echo "Please enter one of the following options"
echo "1. Move empty files"
echo "2. Check file size"
echo "3. Which file is newer"
echo "4. File check rwx"
echo "5. Exit".
echo -e "Enter Choice:"
read answer
case "$answer" in
1) ./move_empty
exit 55 ;;
2) echo "Enter a filename"
read filename
if [ -f $filename ];
then ./file_size
fi
;;
3) echo "Enter first file:"
read filename
echo "Enter second file:"
read filename2
if [ ! -f "$filename" ];
then
echo "Supplied file name" $filename "does not exist";
if [ $filename" -nt $filename" ]; then
echo $filename "is newer"
exit 1fi
fi ;;
5) exit ;;
esac
done
If you have completed the check at ShellCheck.net, then you should have received:
$ shellcheck myscript
No issues detected!
If you didn't work it down to that point, you are not done. You have multiple quoting problems in your script and you compare $filename -nt $filename (which is always false). Small "attention to detail" issues that make a big difference. ShellCheck.net does a thorough job, but will not find logic issues, those are left to you. The cleanup of your quoting would look similar to:
#!/bin/bash
while true
do
clear
echo "Please enter one of the following options"
echo "1. Move empty files"
echo "2. Check file size"
echo "3. Which file is newer"
echo "4. File check rwx"
echo "5. Exit".
echo -n "Enter Choice: "
read -r answer
case "$answer" in
1) ./move_empty
exit 55
;;
2) echo -n "Enter a filename: "
read -r filename
if [ -f "$filename" ]
then
./file_size
fi
;;
3) echo -n "Enter first file: "
read -r filename
echo -n "Enter second file: "
read -r filename2
if [ ! -f "$filename2" ]
then
echo "Supplied file name $filename does not exist";
if [ "$filename" -nt "$filename2" ]; then
echo "$filename is newer"
exit 1
fi
fi
;;
5) exit
;;
esac
done
(note: you do not need echo -e as there are no backslash escaped characters to handle in your prompt, likely you intended -n to prevent the addition of a newline at the end of the prompt)
(also note: the use of clear, while fine for some terminals, will cause problems with others. Just be aware of the potential issue.)
If your then is on the same line with your conditional expression, e.g. if [ "$filename" -nt "$filename2" ]; then then a ';' is needed after the closing ']' to indicate a newline, otherwise, there is no need for a ';'.
Logic Problems
As discussed, the logic problems are not caught by ShellCheck and you must work though the code. It looks like you intended something like the following:
3) echo -n "Enter first file: "
read -r filename
echo -n "Enter second file: "
read -r filename2
if [ ! -f "$filename" ] || [ ! -f "$filename2" ]
then
echo "Supplied file '$filename' or '$filename2' does not exist";
exit 1
fi
if [ "$filename" -nt "$filename2" ]; then
echo "$filename is newer"
else
echo "$filename2 is newer"
fi
;;
You just have to take it line by line...
Look things over and let me know if you have further questions.
I am a newbie to stack overflow so please bear with the formatting
It is a genuine problem
I am creating a program to copy ,remove and move a file but still cannot resolve the echo error that bugs it, what should i do?
I have read the other related question which tells to include a ; but this too has not resolved the situation
#!/bin/bash
echo "Menu "
echo "1. Copy a File "
echo "2. Remove a file "
echo "3. Move a file"
echo "4. Quit"
echo "Enter ur Choice \c"
read Choice
case " $Choice " in
1. echo "Enter File name to copy \c"
read f1
echo " Enter File name \c "
read f2
if [ -f $f1 ]
then
cp $f1 $f2
else
echo "$f1 does not exist"
fi
;;
2. echo "Enter the File to be removed "
read r1
if [ -f $r1 ]
then
rm -i $r1
else
echo "$r1 file does not exist "
fi
;;
3.
echo "Enter File name to move \c"
read f1
echo "Enter destination \c "
read f2
if [ -f $f1 ]
then
if [ -d $f2 ]
then
mv $f1 $f2
fi
else
echo "$f1 does not exist"
fi
;;
4.
echo "Exit......."
exit;;
esac
ERRORS
./script13.sh: line 10: syntax error near unexpected token `echo'
./script13.sh: line 10: ` 1. echo "Enter File name to copy \c " '
root#Kalilinux1:~/bin#
You lack the parenthesis, so :
#!/bin/bash
echo "Menu "
echo "1. Copy a File "
echo "2. Remove a file "
echo "3. Move a file"
echo "4. Quit"
read -p "Enter ur Choice >>> "
case " $Choice " in
1) echo "Enter File name to copy \c"
read f1
echo " Enter File name \c "
read f2
if [ -f $f1 ]
then
cp $f1 $f2
else
echo "$f1 does not exist"
fi
;;
2) echo "Enter the File to be removed "
read r1
if [ -f $r1 ]
then
rm -i $r1
else
echo "$r1 file does not exist "
fi
;;
3)
echo "Enter File name to move \c"
read f1
echo "Enter destination \c "
read f2
if [ -f $f1 ]
then
if [ -d $f2 ]
then
mv $f1 $f2
fi
else
echo "$f1 does not exist"
fi
;;
4)
echo "Exit......."
exit;;
esac
Instead of 1. in line 10, you should use 1). The correct way to do a case statement is:
case "$Choice" in
1) ...
;;
2) ...
;;
Also, remove the spaces in " $Choice ".
This code is that I want to give two directory and this code will look if these two directory contains two files that contains the same information, and asks me which file I want to delete .
I have written this code but I don't know how to write the code that will delete the file , please help
#!bin/bash
echo "give the first directory"
read firstdir
echo "give the second directory"
read seconddir
for i in ` ls $firstdir`
do
echo $i
t= `md5sum $firstdir/$i`
s= `md5sum $seconddir/$i`
if [ "$t" ! = "$s" ]
then
echo " of which directory will be eliminated? $i"
read direct
( here I want the code to delete the directory ex : delete direct/i )
fi
done
Replace:
echo " of which directory will be eliminated? $i"
read direct
( here I want the code to delete the directory ex : delete direct/i )
With:
echo "of which directory will be eliminated? $i:
1)$firstdir
2)$seconddir
"
read -p "(1/2)" direct
case $direct in
1)
rm -v $firstdir/$i
;;
2)
rm -v $seconddir/$i
;;
*)
echo "ERROR: bad value, 1 or 2 expected!" ; exit 1
esac
Ok, try this. I just made my own solution based on your requirements. I hope you like it. Thanks
#!/bin/bash
# check for a valid first directory
while true
do
echo "Please, enter the first directory"
read FIRST_DIR
if [ -d $FIRST_DIR ]; then
break
else
echo "Invalid directory"
fi
done
# check for a valid second directory
while true
do
echo "Please, give the second directory"
read SECOND_DIR
if [ -d $SECOND_DIR ]; then
break
else
echo "Invalid directory"
fi
done
for FILE in `ls $FIRST_DIR`
do
# make sure that only files will be avaluated
if [ -f $FILE ]; then
echo $SECOND_DIR/$FILE
# check if the file exist in the second directory
if [ -f $SECOND_DIR/$FILE ]; then
# compare the two files
output=`diff -c $FIRST_DIR/$FILE $SECOND_DIR/$FILE`
if [ ! $output ]; then
echo "Which file do you want to delete?
1)$FIRST_DIR/$FILE
2)$SECOND_DIR/$FILE
"
# read a choice
read -p "(1/2)" choice
# delete the chosen file
case $choice in
1)
rm -v $FIRST_DIR/$FILE
;;
2)
rm -v $SECOND_DIR/$FILE
;;
*)
echo "ERROR invalid choice!"
esac
fi
else
echo "There are no equal files in the two directories."
exit 1
fi
else
echo "There are no files to be evaluated."
fi
done