How to auto correct a failing command?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to auto correct a failing command?
# 1  
Old 01-19-2017
Hammer & Screwdriver How to auto correct a failing command?

If a command is not found, e.g. nawk, this is how I fix the problem
Code:
[[ -x /usr/bin/gawk ]] && NAWK=/usr/bin/gawk  
[[ -x /usr/bin/nawk ]] && NAWK=/usr/bin/nawk  
[[ -x /usr/bin/awk ]] && NAWK=/usr/bin/awk

I use $NAWK an the set the appropriate value based on the system it runs.

How can I implement a similar fix for a command found but illegal argument. See pseudo example below.

Code:
grep -o hello *
grep: invalid option -- o


Last edited by rbatte1; 01-20-2017 at 06:53 AM.. Reason: Grammar
# 2  
Old 01-19-2017
If computer software could do that, it wouldn't really need you.

You need to rewrite that section without using nonportable options for it to be portable.
# 3  
Old 01-19-2017
But sometimes one can do someting with nonportable options.
One example is the echo command. For ages I used echo \t\b\n to echo tab, bel or newline. But this does not work anymore in bash. If you want echo to interpret escaped characters, you must use the -e option: echo -e \t\b\n .
So portable scripts often use this construction:
Code:
if echo '\t' |grep t >/dev/null 
then   
  # -e option needed to interpret \t \n etc.
  ECHOOPTS=-e
else   
  # No options needed
  ECHOOPTS=""
fi
echo $ECHOOPTS "col1\tcol2\tcol3\n"

So this is what you can do: use variables to store options.
# 4  
Old 01-19-2017
See, I don't think that actually helps you. There are systems where echo -e literally prints -e. Theoretically this script could detect and warn you when your code is broken, but it doesn't actually make your code portable. There's places -e is needed, places it's not, and places where echo never does what you want.

If you want your script to be portable, write it portably.

There is a command which handles escapes properly everywhere, in every shell: printf. Rewrite using printf instead of echo -e. Just a blind substitution won't work of course, you need to add an \n.

As an alternative for grep -o , awk.

Last edited by Corona688; 01-19-2017 at 06:36 PM..
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 01-19-2017
Hammer & Screwdriver

Quote:
Originally Posted by Corona688
See, I don't think that actually helps you. There are systems where echo -e literally prints -e. Theoretically this script could detect and warn you when your code is broken, but it doesn't actually make your code portable. There's places -e is needed, places it's not, and places where echo never does what you want.

If you want your script to be portable, write it portably.

There is a command which handles escapes properly everywhere, in every shell: printf. Rewrite using printf instead of echo -e. Just a blind substitution won't work of course, you need to add an \n.

As an alternative for grep -o , awk.
My requirement is less to play with the syntax of the command and moreso to use a different utility like ggrep or GNU grep for failing commands.

Can you suggest a good similar feasible solution ?
# 6  
Old 01-19-2017
If you want code that will work on a variety of systems, the only way to do that portably is to write portable code to begin with.

You can't magically take a script that works on AIX systems using options that are only available on AIX and make it work on HP-UX, Solaris, and Linux systems. You can't magically take a script that works on HP-UX systems using options that are only available on HP-UX systems and make it work on AIX, Solaris, and Linux systems. You can't magically take a script that works on Solaris systems using options that are only available on Solaris systems and make it work on AIX, HP-UX, and Linux systems. You can't magically take a script that works on Debian Linux systems using options that are only available on Debian Linux systems and make it work on AIX, HP-UX, Solaris, and Red Hat Linux systems. ...

But, you can write a strictly conforming POSIX shell script and have it work perfectly on AIX, HP-UX, Solaris, and OS X systems (and if you're lucky, there is also a good chance that it will also work on many Linux systems and on many BSD systems).
# 7  
Old 01-20-2017
He hasn't said so explicitly, but I've gained the impression that mohtashims has been required to maintain and port a lot of shell scripts not all his own devising, and hopes for cookie-cutter solutions for scripts written using non-portable options.

There a few strategies you can try.
  1. Substituting different commands on different systems.
  2. Building your own substitutes for every troublesome command.
  3. Re-factoring the scripts to use actually portable options.

Solution #1 obviously looks easier and more attractive, since hypothetically it can be done with just a "header" section and few to no changes to large amounts of code you may not necessarily understand. But if it really was better, you would have solved your problems months ago. You must be dealing with a large number of very different systems, as you're still finding and plugging hole after hole, and will continue to do so for a long time. What will you do when you hit one which doesn't have GNU extensions available?

Solution #2 is a big job. I've spent large amounts of time duplicating one command.

Solution #3 unfortunately means having to understand what grep -o actually means, so as to replace it. You may want to break it down and test commands individually rather than fighting it as one gigantic block of code.

My last comment: This is what happens when you try and detect and compensate for every difference between systems:
Image
That's 800 kilobytes of ad-hoc corrections per decade, more or less.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

LS command does not list the correct file

Hi, I am logged into as root & inside the home directory of another user. ls -a # ls -laq total 44 drwx------ 4 user1 adm 4096 Nov 23 05:10 . drwxr-xr-x. 12 root root 4096 Nov 22 13:05 .. -rw-r--r-- 1 user1 adm 18 Nov 22 13:05 .bash_logout -rw-r--r-- 1 user1 adm 193 Nov... (2 Replies)
Discussion started by: mohtashims
2 Replies

2. UNIX for Advanced & Expert Users

While trying to load .so file manually using command its failing

Hi all, I am newbie to linux environment. I was trying to run an .so file manually which in turn call a method in bin folder. Command given, XXX_MODULES=libxxx.so /opt/servicename/bin/methodname -Le -c /opt/servicename/etc/methodname/methodname.conf -n -C -t -m "" When i tried to execute... (1 Reply)
Discussion started by: sharathpadman
1 Replies

3. UNIX for Dummies Questions & Answers

While trying to load .so file manually using command its failing

Hi all, I am newbie to linux environment. I was trying to run an .so file manually which in turn call a method in bin folder. Command given, XXX_MODULES=libxxx.so /opt/servicename/bin/methodname -Le -c /opt/servicename/etc/methodname/methodname.conf -n -C -t -m "" When i tried to... (1 Reply)
Discussion started by: sharathpadman
1 Replies

4. Shell Programming and Scripting

Auto correct a csv file using UNIX shell script.

Hi All, There are list of 4-5 .csv files which has 12 columns.In some cases one of the record is split into 2 records. What needs to be done is this split record has to be auto corrected and placed in the csv file. Eg: Let us consider sample.csv file and in normal conditions the file would... (40 Replies)
Discussion started by: karthik_ak
40 Replies

5. Post Here to Contact Site Administrators and Moderators

Auto correct a csv file using UNIX shell script.

Hi All, There are list of 4-5 .csv files which has 12 columns.In some cases one of the record is split into 2 records. What needs to be done is this split record has to be auto corrected and placed in the csv file. Eg: Let us consider sample.csv file and in normal conditions the file... (1 Reply)
Discussion started by: karthik_ak
1 Replies

6. UNIX for Advanced & Expert Users

I was trying this command...am I going correct? other there is better way

I was trying to copy all debs from apt cache to some storage location and I was taking this approach... /var/cache/apt/archives# ls -1 | grep -v jdownloader | fgrep .deb | xargs cp /media/eshant/L-STORE/Softwares/openjdk/an error bla_bla.deb is a not directory stalled me Suggestions please... (9 Replies)
Discussion started by: ezee
9 Replies

7. Shell Programming and Scripting

For loop failing cd command

Hi guys, i've wrote the following loop; for i in `ls` do cd $i/host cat "xxxx.txt" |grep "yyyy" >> zzzz.txt done I have a set of folder with different name and i need to extract a value from a file contained in the host subfolder ( that is present in each folder). When i run... (4 Replies)
Discussion started by: cecco16
4 Replies

8. Shell Programming and Scripting

tty command failing

We have script like this in the .bash_profile.. #-# determine if session is interactive or in background if ]; then while true; do read -p "Do you wish to load profile yes or no?" yn case $yn in * ) source /opt/oracle/.profile; break;; * ) break;; *... (2 Replies)
Discussion started by: talashil
2 Replies

9. UNIX for Dummies Questions & Answers

Is this grep command correct?

Hi I need to know if I have got the following grep command correct. I wanted to find from myfile.txt all signed real numbers (e.g. +5.0, -78, but not 5.0, 5 are not). I have thought about it and decided it was this: grep '' myfile.txt but I was wondering if this was correct. If... (2 Replies)
Discussion started by: rushhour
2 Replies

10. HP-UX

dd command failing

I am new to HP-UX. I have an 8GB drive that is my root drive, contained in a Volume Group. I would like to clone that drive to another drive, which is 18.4GB. The other drive is not in a volume group. I am using this simple command:# dd if=/dev/dsk/c0t6d0 of=/dev/dsk/c0t5d0The command... (4 Replies)
Discussion started by: emsecrist
4 Replies
Login or Register to Ask a Question