3
\$\begingroup\$
  • up moves N directories above the current directory (executes "cd .."" N times)
  • When executed without input values it moves only one directory above the current directory
  • When executed with input values, up uses the first input value (checking to see if it is a positive integer)to move above the current directory the specified number of directories
up() {
 if [ -z "1ドル" ]
 then 
 eval "cd .."
 else
 if [ "1ドル" -eq "1ドル" ]
 then
 if [ "1ドル" -gt "0" ]
 then
 for i in `seq 1 1ドル`;
 do
 eval "cd .."
 done 
 eval "pwd"
 else
 echo "First argument is not a positive integer"
 fi
 else
 echo "First argument is not an integer"
 fi
 fi
}

I did consider building a cd ../../.. etc., chain which would execute after the for loop builds the required string and would also preserve the usefulness of cd - but this version was slightly cleaner.

The [ "1ドル" -eq "1ドル" ] thing is one way to check if a value is an integer.

asked Nov 26, 2015 at 19:34
\$\endgroup\$
0

1 Answer 1

2
\$\begingroup\$

If you're writing this as a shell function, then you don't need to eval.

The indentation could be done more idiomatically. By using elif, you can eliminate one level of indentation and make the code structure clearer.

up() {
 if [ -z "1ドル" ]; then 
 cd ..
 elif [ "1ドル" -eq "1ドル" ]; then
 if [ "1ドル" -lt 0 ]; then
 echo "First argument is not a positive integer"
 else
 for i in `seq 1 1ドル`; do
 cd ..
 done
 pwd
 fi
 else
 echo "First argument is not an integer"
 fi
}

Your question is very similar to this one. The main problem, as you have already noted in your own comment, is that by doing multiple cd .. in sequence instead of one cd ../../.., you would be inserting many hops into the directory history. Then, cd - or referencing $OLDPWD wouldn't work as expected.

answered Nov 26, 2015 at 19:51
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.