Skip to main content

I am trying to get this bash script written in only one line. Mostly for fun's sake. The script basically compiles a .c script if($1=0)if($1=0). And if($1=1)if($1=1), it checks to see if lifemp exists, if it does not, it compiles it, then runs lifemp with the 2nd - 5th2nd - 5th command line arguments.

Here is the working script with normal if statements:

#!/bin/bash cd gol if [ $1 -eq 0 ]; then make clean make lifemp USE_FILE=$2 elif [ $1 -eq 1 ]; then if [ ! -f lifemp ]; then make lifemp fi ./lifemp $2 $3 $4 $5 fi 

And here is what I have come up with for it only on one line. If $1 = 0$1 = 0, this runs fine. The problem comes when $1 = 1$1 = 1. I think it has something to do with me having a nested ternary expression to check if the lifemp file exists or not.

#!/bin/bash cd gol && eval `[[ $1 = "1" ]] && (echo [[ ! -f lifemp ]] && (echo "make lifemp && ./lifemp $2 $3 $4 $5") || ("./lifemp $2 $3 $4 $5")) || (echo "make clean && make lifemp USE_FILE=$2")` 

If anyone wants to rack their brain with me to try and figure it out, I would be very appreciative!

Thanks!

I am trying to get this bash script written in only one line. Mostly for fun's sake. The script basically compiles a .c script if($1=0). And if($1=1), it checks to see if lifemp exists, if it does not, it compiles it, then runs lifemp with the 2nd - 5th command line arguments.

Here is the working script with normal if statements:

#!/bin/bash cd gol if [ $1 -eq 0 ]; then make clean make lifemp USE_FILE=$2 elif [ $1 -eq 1 ]; then if [ ! -f lifemp ]; then make lifemp fi ./lifemp $2 $3 $4 $5 fi 

And here is what I have come up with for it only on one line. If $1 = 0, this runs fine. The problem comes when $1 = 1. I think it has something to do with me having a nested ternary expression to check if the lifemp file exists or not.

#!/bin/bash cd gol && eval `[[ $1 = "1" ]] && (echo [[ ! -f lifemp ]] && (echo "make lifemp && ./lifemp $2 $3 $4 $5") || ("./lifemp $2 $3 $4 $5")) || (echo "make clean && make lifemp USE_FILE=$2")` 

If anyone wants to rack their brain with me to try and figure it out, I would be very appreciative!

Thanks!

I am trying to get this bash script written in only one line. Mostly for fun's sake. The script basically compiles a .c script if($1=0). And if($1=1), it checks to see if lifemp exists, if it does not, it compiles it, then runs lifemp with the 2nd - 5th command line arguments.

Here is the working script with normal if statements:

#!/bin/bash cd gol if [ $1 -eq 0 ]; then make clean make lifemp USE_FILE=$2 elif [ $1 -eq 1 ]; then if [ ! -f lifemp ]; then make lifemp fi ./lifemp $2 $3 $4 $5 fi 

And here is what I have come up with for it only on one line. If $1 = 0, this runs fine. The problem comes when $1 = 1. I think it has something to do with me having a nested ternary expression to check if the lifemp file exists or not.

#!/bin/bash cd gol && eval `[[ $1 = "1" ]] && (echo [[ ! -f lifemp ]] && (echo "make lifemp && ./lifemp $2 $3 $4 $5") || ("./lifemp $2 $3 $4 $5")) || (echo "make clean && make lifemp USE_FILE=$2")` 

If anyone wants to rack their brain with me to try and figure it out, I would be very appreciative!

Thanks!

Source Link
Sentenial
  • 61
  • 1
  • 10

Bash one-liner using ternary expressions

I am trying to get this bash script written in only one line. Mostly for fun's sake. The script basically compiles a .c script if($1=0). And if($1=1), it checks to see if lifemp exists, if it does not, it compiles it, then runs lifemp with the 2nd - 5th command line arguments.

Here is the working script with normal if statements:

#!/bin/bash cd gol if [ $1 -eq 0 ]; then make clean make lifemp USE_FILE=$2 elif [ $1 -eq 1 ]; then if [ ! -f lifemp ]; then make lifemp fi ./lifemp $2 $3 $4 $5 fi 

And here is what I have come up with for it only on one line. If $1 = 0, this runs fine. The problem comes when $1 = 1. I think it has something to do with me having a nested ternary expression to check if the lifemp file exists or not.

#!/bin/bash cd gol && eval `[[ $1 = "1" ]] && (echo [[ ! -f lifemp ]] && (echo "make lifemp && ./lifemp $2 $3 $4 $5") || ("./lifemp $2 $3 $4 $5")) || (echo "make clean && make lifemp USE_FILE=$2")` 

If anyone wants to rack their brain with me to try and figure it out, I would be very appreciative!

Thanks!