Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I made a shell script which is offering menu choice. I made it on RHEL & then with little bit changes I was able to run successfully on AIX/ksh.
Script is working fine with no issues other than a little one i.e., There is one choice in which I can list out and delete some files from a directory as per userinput. If there are files it listing and deleting without any problem but if there ain't any files then its displaying one error.
i.e., find: 0652-019 The status on 1_*.* is not valid.
Here is my script:
Code:
#!/usr/bin/ksh
script_menu() {
while :
do
clear
echo ----------------------------------------------
echo ************Script Start Menu************
echo ----------------------------------------------
echo Run from the following Scripts
echo
echo [1] xyz.sh
echo [2] abc.sh
echo [3] Return to Main Menu
echo [4] Exit
echo ----------------------------------------------
read choice
case $choice in
1)
echo Run xyz script file; /backup/test1/xyz.sh;
echo Press Enter;
read x;;
2)
echo Run abc script file; /backup/test1/abc.sh;
echo Press Enter;
read x;;
3)
return;;
4)
exit;;
esac
done
}
#Main Menu
while :
do
clear
echo ----------------------------------------------------
echo ******************Main Menu******************
echo ----------------------------------------------------
echo Select a Choice
echo [1] Run the Scripts
echo [2] View the Current Run Numbers
echo [3] Amend the Run Numbers
echo [4] Check Status of propagation
echo [5] Turn Propagation on or off
echo [6] View Log Files
echo [7] List files in import directory
echo [8] Clear files from import directory
echo [9] Exit
echo ----------------------------------------------------
echo Select choice {1-9}:
read choice
case $choice in
1)
script_menu;;
2)
echo Current Run Number; cat /backup/test1/run_number.txt;
echo Press Enter;
read x;;
3) echo Enter Run Number;
read -r userinput; echo $userinput > run_number.txt;
echo Press Enter;
read x;;
4)
echo Propagation Status ; cat /backup/test1/auto_pro.txt;
echo Press Enter;
read x;;
5)
echo Change Propagation ; echo Enter Value; read -r userinput; echo $userinput > auto_pro.txt;
echo Press Enter;
read x;;
6)
echo View Log files;
echo Enter Log file name;
read -r filename ; echo $filename | ls -l | find "$filename"_*.* -type f ! -name ".*" | awk '{print}';
echo Press Enter;
read x;;
7)
echo List files in import directory;
echo Enter file name;
read -r filename ; echo $filename | ls -l | find "$filename"_*.* -type f ! -name ".*" | awk '{print}';
echo Press Enter;
read x;;
8)
echo Clear files from import directory;
echo Enter File Name;
read -r filename ; echo $filename | ls -l | find "$filename"_*.* -type f ! -name ".*" -exec rm -f {} \;
echo Press Enter;
read x;;
9)
exit;;
*)
echo Invalid Number
esac
done
option number 6,7,8 are giving above error, If there is no files with the name as input by user.
How can I solve this? Plz help !!
As per my understanding, command is working fine i.e., find command. B'coz on RHEL machine its showing: 5_*.*: No such file or directory
but on AIX machine its showing: find: 0652-019 The status on 1_*.* is not valid.
I guess context is the same of both saying.
So what I'm thinking to do(now) that How can I put a customized message like: "No file with this name", when there is no file.
Plz help me with that.
I think noone understand your script. What should do this line:
Code:
read -r filename ; echo $filename | ls -l | find "$filename"_*.* -type f ! -name ".*" | awk '{print}';
ls does not accept anything from stdin, so here: echo $filename | ls -l the red part is useless. also the | before find is meaningless. | awk '{print}' does nothing.
awk '{print}'is useless that i came to know. But ls -l is taking input from echo $filename and its printing the filenames only if the files are there. and if the files are not there it printing: 1_*.*: No such file or directory on RHEL plateform. find: 0652-019 The status on 1_*.* is not valid. on AIX plateform.
problem is this i need to print some customized message instead of these messages. Like in Simple plain english it should say. File is not present or something like that.
how to print that my question is that...
Hope you got my point.
ls -l is taking input from echo $filename and its printing the filenames only if the files are there.
executing echo <anything> | ls -l will give the same result as ls -l alone (at least on RH, but I think on AIX also).
If you think it is not the case just show me how it works.
find also cannot handle pipe as input. If you want to check if $filename was valid, you would try [[ -f "$filename" ]] && find .... If you want to check if it was a directory, type [[ -d "$filename" ]] && find .....
thanks alot @pan64. u r absolutely ryt. I was using some unnecessary commands. i.e., ls,echo,awk. I'm a newbie in scripting. so from these type of experiences I will learn. as today I learned from your reply.
and somehow searching I made it to run.
again thanks for your help.
_____________________________________
If someone helps you, or you approve of what's posted, click the "Add to Reputation" button, on the left of the post.
Happy with solution ... mark as SOLVED
(located in the "thread tools")
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.